Client Script Cheat Sheet


Server loopups
It is important to minimize server calls, especially you should NOT use a direct GlideRecord lookup nor a g_form.getReference() (if you use the getReference(), remember to use a callback). Instead you should use g_scratchpad or the asynchronous GlideAjax.
g_scratchpad client script

Use the g_scratchpad to pass information to the client side, when the form loads, to reduce the load time.

//*** 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

An onDisplay business rule to prepare all the data for client side on g_scratchpad

//*** 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

Call the asynchronous script include from the client script, and also receive the answer back from the server after processing

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

Asynchronous GlideAjax script include, called from the client script with the script include name and the sysparm_name of the function.

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, remember to 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);