Minimize server lookups

This article is based on the ServiceNow documentation article. See the original article on the ServiceNow doc site: Minimize Server Lookups

Client scripting uses either data available on the client or data retrieved from the server. Use client data as much as possible to eliminate the need for time-consuming server lookups. The top ways to get information from the server are g_scratchpad, and asynchronous GlideAjax lookup.

The primary difference between these methods is that g_scratchpad is sent once when a form is loaded (information is pushed from the server to the client), whereas GlideAjax is dynamically triggered when the client requests information from the server.

Other methods, GlideRecord and g_form.getReference() callback are also available for retrieving server information. However, these methods are no longer recommended due to their performance impact. Both methods retrieve all fields in the requested GlideRecord when most cases only require one field. The GlideRecord API is not available for scoped applications.

Example: g_scratchpad

The g_scratchpad object passes information from the server to the client, such as when the client requires information not available on the form. For example, if you have a Client Script which needs to access the field u_retrieve, and the field is not on the form, the data is not available to the Client Script. A typical solution to this situation is to place the field on the form and then always hide it with a Client Script or UI Policy. While this solution may be faster to configure, it is slower to execute.

If you know what information the client needs from the server before the form is loaded, a display Business Rule can create g_scratchpad properties to hold this information. The g_scratchpad object is sent to the client when the form is requested, making it available to all client-side scripting methods. This is a very efficient means of sending information from the server to the client. However, you can only load data this way when the form is loaded. The Business Rule cannot be triggered dynamically. In those cases, use an asynchronous GlideAjax call.

For example, assume you open an incident and need to pass this information to the client:

  • The value of the system property css.base.color
  • Whether or not the current record has attachments
  • The name of the caller’s manager

A display Business Rule sends this information to the client using the following script:

g_scratchpad.css = gs.getProperty('css.base.color');
g_scratchpad.hasAttachments = current.hasAttachments();
g_scratchpad.managerName = current.caller_id.manager.getDisplayValue();

Example: Asynchronous GlideAjax

This script compares the support group of the CI and the assignment group of the incident by name:

//Alert if the assignment groups name matches the support group
function onChange(control, oldValue, newValue, isLoading) {
 
    if (isLoading)
        return;

    var ga = new GlideAjax('ciCheck');

    ga.addParam('sysparm_name', 'getCiSupportGroup');
    ga.addParam('sysparm_ci', g_form.getValue('cmdb_ci'));
    ga.addParam('sysparm_ag', g_form.getValue('assignment_group'));
    ga.getXML(doAlert); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
}

// Callback function to process the response returned from the server
function doAlert(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");

    alert(answer);
}

This script relies on an accompanying script include, such as:

var ciCheck = Class.create();

ciCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getCiSupportGroup: function() {
        
        var retVal = ''; // Return value
        var ciID   = this.getParameter('sysparm_ci');
        var agID   = this.getParameter('sysparm_ag');		
        var ciRec  = new GlideRecord('cmdb_ci');
        
        // If we can read the record, check if the sys_ids match
        if (ciRec.get(ciID)) {
            if (ciRec.getValue('support_group') == agID)
                retVal = 'CI support group and assignment group match';
            else
                retVal = 'CI support group and assignment group do not match';
            
            // Can't read the CI, then they don't match
        } else {
            retVal = 'CI support group and assignment group do not match';
        }
        
        return retVal;
    }
    
});

Use setValue()’s displayValue Parameter with Reference Fields

When using setValue() on a reference field, be sure to include the display value with the value (sys_id). If you set the value without the display value, ServiceNow does a synchronous Ajax call to retrieve the display value for the record you specified. This extra round trip to the server can leave you at risk of performance issues.

Incorrect:

var id = '5137153cc611227c000bbd1bd8cd2005';

g_form.setValue('assigned_to', id); 
// Client needs to go back to the server to
// fetch the name that goes with this ID

Correct:

var id = '5137153cc611227c000bbd1bd8cd2005';
var name = 'Fred Luddy';

g_form.setValue('assigned_to', id, name); // No server call required

What's here


Related content

ServiceNow: Minimize Server Lookups




Last modified on Jun 15, 2020