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.
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.
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.
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.
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.
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.
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.
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.
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.