ServiceNow Certification Mock Exams
Click To View Now
SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
How To Dot-Walk Inside of a Client Script in ServiceNow
How To's

How To Dot-Walk Inside of a Client Script in ServiceNow

How To Dot-Walk Inside of a Client Script in ServiceNow

Introduction

Dot-walking is an important feature in ServiceNow used to access related fields and values for reference fields.

Dot-walking is commonly used when scripting in ServiceNow to get the value of a field that is not present on the record you are currently working with. In the following example for the Incident table, we are able to access the Caller’s Email in a server-side script using dot-walking.

 current.caller_id.email;

A frequently asked question by developers regarding dot-walking is how to dot-walk inside of a Client Script in ServiceNow? The short answer is… you don’t.

If you attempt to dot-walk inside of a Client Script in ServiceNow (using something like the below example) it simply will not work.

Instead, there are two potential methods for achieving this functionality. These methods are either using GlideAjax and a Script Include or getReference with a callback function.

In this blog we will discuss both techniques as well as provide examples for developers in order to properly demonstrate how to dot-walk inside of a Client Script in ServiceNow.

 g_form.getValue('caller_id.email'); // WILL NOT WORK

Using getReference with a callback function

The first method we will discuss for dot-walking inside of a Client Script in ServiceNow is using getReference with a callback function.

This is the “simpler” method, but not the recommended one. In this scenario all the scripting is done within the Client Script record. Upon execution, an asynchronous call will be made to the server, while the client-side browser continues to process and wait for a response from the server.

Once the server returns the reference value, the code within the callback function will execute.

Example of getReference/callback function

The following getReference example is for an onChange Client Script that runs on the Incident table when the Caller field is changed. In this case we are looking for the Caller’s email.

 onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.getReference('caller_id', getEmail); // getEmail is the callback function

    function getEmail(caller) { // Caller reference is passed into callback
        alert('Caller email: ' + caller.email); // Dot-walking is done in callback function
    }
}

It is important to call out that using this method could have a potential negative performance impact on the instance. Since a call to the server is required, there is additional processing time which may lead to latency on the client-side. That is why this is not the recommended approach for dot-walking in a Client Script. Be sure to keep this in mind when developing.

Using GlideAjax and a Script Include

The second method we will discuss for dot-walking inside of a Client Script in ServiceNow is using GlideAjax along with Script Include.

This is the preferred method when it comes to performance. Although this process is slightly more complex, the advantage of GlideAjax is that it enables a client script to call server-side code that lives in a Script Include.

For this approach, you declare a GlideAjax variable in the Client Script and specify which Script Include, function and data you would like to pass (via parameters) over to the server. The Script Include will then execute the code on the server and return data back to the client.

Once you have received that data from the server, you can do whatever you want with it in the Client Script.

Example of GlideAjax/Script Include

This GlideAjax example is using the same scenario as above of an onChange Client Script that runs on the Incident table when the Caller field is changed. For this scenario we are returning the Caller’s Manager’s sys_id.

Client Script

 function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var gaCaller = new GlideAjax('GetUserInfo'); // Specify Script Include name 
    gaCaller.addParam('sysparm_name', 'getManager'); // Parameter is the function that we're calling 
    gaCaller.addParam('sysparm_user', g_form.getValue('caller_id')); // Parameter is caller's sys_id 
    gaCaller.getXML(callback);
    // getXML sends the server a request to execute the function and parameters associated with this GlideAjax object
    // Server processes the request asynchronously and returns the results via the function specified as the callback

    // Callback function for returning the result from the Script Include
    function callback(response) {
        var managerID = response.responseXML.documentElement.getAttribute("answer");
        // Do whatever you want with the response
        alert("Manager's sysid: " + managerID);
    }
}

Script Include

 var GetUserInfo = Class.create();
GetUserInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getManager: function() {
        var user = this.getParameter("sysparm_user");
        var grUser = new GlideRecord('sys_user');
        grUser.get(user);

        // This is where dot-walking is done
        // In this case we return the manager's sys_id
       return grUser.manager;
    },
    type: 'GetUserInfo'
});

With this method dot-walking takes place in the Script Include. Again, this is the preferred method when it comes to performance. If we needed to return multiple user record fields related to the Caller, we could build an object in the Script Include and return that to the client as shown in the next example. The new function was added to the GetUserInfo Script Include.

Client Script

 function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var gaCaller = new GlideAjax('GetUserInfo');
    gaCaller.addParam('sysparm_name', 'getManagerandEmail');
    gaCaller.addParam('sysparm_user', g_form.getValue('caller_id'));
    gaCaller.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        answer = JSON.parse(answer);
        // Do whatever you want with the response
        alert(answer.manager + " " + answer.email);
    }
}

Script Include

     getManagerandEmail: function() {
        var user = this.getParameter("sysparm_user");
        var grUser = new GlideRecord('sys_user');
        grUser.get(user);

        // Build the payload
        // In this case we return the manager's sys_id and user's email 
        var result = {
            "manager": grUser.manager.toString(),
            "email": grUser.email.toString()
        };
        // return JSON string of the object
        return JSON.stringify(result);
    },

    type: 'GetUserInfo'

Conclusion

In this blog we have covered how to dot-walk inside of a Client Script in ServiceNow.

You can choose either of the methods discussed, but remember GlideAjax is the preferred approach. One last important note for developers is to NEVER use GlideRecord when scripting client-side.

Snowycode team
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Read more