Client Script Cheat Sheet
client script, cheat sheet
Server lookups
It is important to minimize server calls. You should not use a direct GlideRecord lookup nor g_form.getReference() (if you use getReference, remember to use a callback). Prefer g_scratchpad or asynchronous GlideAjax.
g_scratchpad client script
//*** Client script ***
if(g_scratchpad.hasAttachments && g_scratchpad.instance == "production") {
var comment = "Something is attached to a child of " + parentCaller + "'s record.";
g_form.setValue('comments', comment);
}
g_scratchpad business rule (onDisplay)
//*** onDisplay business rule ***
g_scratchpad.hasAttachments = current.hasAttachments(); //Does this record have attachments?
g_scratchpad.instance = gs.getProperty('instance_name'); //Get the name of the instance
g_scratchpad.parentCaller = current.parent.caller_id.name; //Get parent's caller's name
GlideAjax client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var ga = new GlideAjax('serverCheck');
ga.addParam('sysparm_name', 'serverFunctionName'); //sysparm_name is reserved for the name of the script include function to call
ga.addParam('sysparm_group', g_form.getValue('assignment_group'));
ga.addParam('sysparm_other_variable', g_form.getValue('short_description')); //Any variable can be sent
ga.getXML(clientCallback); //Use getXML rather than getXMLWait. Input is your callback function
}
function clientCallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); //Get answer from server
//Do something with answer
g_form.setValue('short_description', answer);
}
GlideAjax script include
var serverCheck = Class.create();
serverCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
serverFunctionName: function() {
var assignment_group = this.getParameter('sysparm_group');
var short_desc = this.getParameter('sysparm_other_variable');
var returnValue = "";
var groups = new GlideRecord('sys_user_group');
if(groups.get(assignment_group)) {
returnValue = groups.name + ": " + short_desc;
}
return returnValue;
}
});
setValue()
When setting a reference value, include the display value, otherwise ServiceNow will make another call to the server.
//Reference field
var group_name = 'IT Support';
var group_id = '86e8d203ff833100ba13ffffffffff70';
g_form.setValue('assignment_group', group_id, group_name); //No server call required
//Other fields
var short_desc = "This is my new short description";
g_form.setValue('short_description', short_desc);