Simplify your GlideAjax syntax
November 29, 2024
There are lot of tutorials out there on how to write GlideAjax code in ServiceNow. And yet another guy comes with another one. This one shows you how to make it simple but effective. This one is my personal preference on how to write GlideAjax the best. And I just feel that it could help somebody to simplify their code and make it more readable.
In documentation, you will firstly see something like this:
var ga = new GlideAjax('HelloWorld'); // HelloWorld is the script include class
ga.addParam('sysparm_name', 'helloWorld'); // helloWorld is the script include method
ga.addParam('sysparm_user_name', "Bob"); // Set parameter sysparm_user_name to 'Bob'
ga.getXML(HelloWorldParse); /* Call HelloWorld.helloWorld() with the parameter sysparm_user_name set to 'Bob' and use the callback function HelloWorldParse() to return the result when ready */
// the callback function for returning the result from the server-side code
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
First thing I prefer to improve is to get answer right away and avoid that super long, non-understandable, line of code with response object (yes, this one - response.responseXML.documentElement.getAttribute).
var ga = new GlideAjax('HelloWorld'); // HelloWorld is the script include class
ga.addParam('sysparm_name', 'helloWorld'); // helloWorld is the script include method
ga.addParam('sysparm_user_name', "Bob"); // Set parameter sysparm_user_name to 'Bob'
ga.getXMLAnswer(HelloWorldParse); /* Call HelloWorld.helloWorld() with the parameter sysparm_user_name set to 'Bob' and use the callback function HelloWorldParse() to return the result when ready */
// the callback function for returning the result from the server-side code
function HelloWorldParse(answer) {
alert(answer);
}
You see? Much simpler! Never use “getXML” anymore! Why should we add unnecessary mess into our codebase? Do not trust some rumors that “getXMLAnswer” is synchronous. It is not. Documentation can prove it. For more detail explanation of differences, please read the great article from Mark Roethof: getAnswerXML vs getXML.
Bonus: Feel free to test this Scan Check that detects all custom scripts using “getXML” instead of “getXMLAnswer”. Ensure to test first in non-production, use at your own risk.
- Update Set DominikSimunek-InstanceScan-getXML-v1.0 - use import of update set to load it to your non-prod instance.
Second thing is to make the callback function more visually linked to the code where it is going to be triggered from. Why should I define callback function separately from where I use it? Why should I even give it a name if I am not going to reuse it ever.
var ga = new GlideAjax('HelloWorld'); // HelloWorld is the script include class
ga.addParam('sysparm_name', 'helloWorld'); // helloWorld is the script include method
ga.addParam('sysparm_user_name', "Bob"); // Set parameter sysparm_user_name to 'Bob'
ga.getXMLAnswer(function (answer) {
alert(answer);
});
Isn’t this better? In my opinion, this is much more readable. I see what will happen once the answer is delivered just below the triggering code (getXMLAnswer). I do not need to look around the whole script to find the callback function.
Let’s close this short blog with more real world example, instead of just the hello world example:
var userAjax = new GlideAjax('UserAjax');
userAjax.addParam('sysparm_name', 'getLocation');
userAjax.addParam('sysparm_user_id', g_form.getValue('caller_id'));
userAjax.getXMLAnswer(function (answer) {
var location = JSON.parse(answer);
g_form.setValue('location', location.sysId, location.displayValue);
});
Notice that:
- I suffix the name of client-callable script include with “Ajax” keyword as I find it good naming convention.
- I do not use verb in the script include name as it is a class and not a function. The verb is rather in the real method name (getLocation).
- I use “JSON.parse” instead of any old “new JSON().parse()” or “new JSONParser().parse()” methods.
- I make sure to set the third parameter when calling “g_form.setValue” to avoid another ajax call to get display value when I was setting only the sys_id.
I hope this helps you - let’s make our ServiceNow scripts more readable together! Let me know if you see further oppotunity to improve this GlideAjax syntax. I wish that this version becomes the official one for Scripting in ServiceNow trainings, as well as documentation, so that we see less of those long, hard-to-read variants.
UPDATE: In the last code example, I replaced "ga" with more explanatory name "userAjax" based on the client-callable script include name which is called. Thanks to Ashish Sharma for the suggestion to improve the variable name "ga" as well compared to what is in documentation.