Where is GlideAjax used in ServiceNow?
Anywhere a script runs in the browser and needs data the page does not have: onChange client scripts (the most common: fill in or check something when a field changes), onLoad client scripts, client-side UI actions (a button, context menu item or link that asks the server something), catalog client scripts on catalog items, record producers and variable sets, and onCellEdit scripts that check a list edit before it is saved. The generator covers all five. Rarer places, such as UI policy scripts and UI pages, use the same pattern: start from Write my own. Service Portal widgets and UI Builder pages do not use GlideAjax; they have their own server scripts and data resources.
What is GlideAjax in ServiceNow?
GlideAjax lets a client script call server code without reloading the form. The client script names a client-callable script include and a method, sends values as sysparm_ parameters, and gets a string back, usually JSON. It is the standard way to read data the form does not have, for example the location of the selected caller.
What is the difference between getXMLAnswer, getXML and getXMLWait?
getXMLAnswer(callback) passes the answer string straight to your callback, which is the simplest. getXML(callback) passes the whole response, and you read the answer attribute yourself. getXMLWait() waits for the server and freezes the form while it does; it is not available in the Service Portal. Use getXMLAnswer.
How do I call a script include from a client script in a scoped app?
Extend global.AbstractAjaxProcessor in the script include, tick Client callable, and call it from the client with its API name, for example new GlideAjax('x_acme_app.CallerInfoAjax'). If the client script is in another scope, set the script include’s Accessible from to All application scopes. Choose “A scoped app” above and the generator does all of this.
Why does my GlideAjax call work for admins but not for other users?
Client-callable script includes can be protected by an access control of type client_callable_script_include with the operation execute. Recent releases may create one when you save the script include, with the roles you choose. If a user does not have one of those roles, the call fails. Add the roles of the people who use the form to that access control.
Is a client-callable script include secure?
Any logged-in user can call it from the browser with any parameters, so treat the parameters as untrusted. Read records with GlideRecordSecure so ACLs apply, use addQuery() rather than building encoded queries from input, and do not add isPublic() returning true unless the data is truly public. The generated code follows all three rules.
When should I not use GlideAjax?
For data the form needs as it opens, a display business rule that fills g_scratchpad avoids a second request. For the current user’s name or roles, g_user already has them. For showing and hiding fields based on values already on the form, a UI policy needs no script at all.
Does the generator work for catalog items, record producers and the Service Portal?
Yes. Choose “A catalog item or record producer” and the script uses your variable names, with setup steps for a catalog client script. To reuse it on several items, attach it to a variable set. The code uses getXMLAnswer and g_form only, which work in the Service Portal and in workspaces. Set UI Type to All.
Can I use GlideAjax in an onSubmit client script?
Not reliably. GlideAjax answers asynchronously, so the save has already gone ahead when the answer arrives, and the synchronous getXMLWait() freezes the form and does not work in the Service Portal. Check the value in an onChange script as the user fills in the form, and stop bad saves on the server with a data policy or a before business rule.