Reference field population via GlideAjax requires both the sys_id AND display value to work correctly. Most developers only return the sys_id, which creates a reference field that shows the sys_id instead of the display value until the record is saved and reloaded. The platform doesn't automatically resolve display values for reference fields set via setValue() — you must provide both values explicitly.
When to use this pattern
- Auto-populating reference fields based on business rules that require server-side data access
- When the lookup logic involves joins or complex queries that exceed client script capabilities
- Populating assignee based on group membership, location-based routing, or skill-based assignment
- When you need to respect ACLs and domain separation during the lookup process
When NOT to use this pattern
- Don't use for simple lookups that can be done with
g_form.getReference()— those execute synchronously on the client - Don't use in Service Portal — use
$httpwith REST API instead for better performance - Don't use when the lookup can return multiple valid results — this pattern assumes one correct answer
- Don't use in onLoad scripts — users haven't made a choice yet, and it creates unnecessary server calls
Key behaviors and gotchas
- Always check
isLoadingparameter to prevent infinite loops when the form loads existing data - GlideAjax responses are always strings — even
nullreturns as the string 'null' - Script Include security: parameters are not sanitized automatically — validate all inputs before using in queries
- Domain separation applies to both the client call and server execution — cross-domain lookups may fail silently
- Use
setLimit(1)when you only need one result — prevents unnecessary database overhead - The three-parameter
setValue(field, value, displayValue)only works for reference fields — regular fields ignore the third parameter
Never use synchronous GlideAjax calls (getXMLWait) in client scripts — they freeze the browser and create a terrible user experience. Always use the asynchronous callback pattern shown above.