Code

Avoid using g_form.getReference()


Performance
2h 30m to fix

What is a reference field in ServiceNow?

In ServiceNow, a reference field is a type of field that connects records between tables by pointing to entries in another table. Reference fields allow you to associate one record with another, facilitating data linkage. When you choose a value in a reference field, it typically corresponds to a specific record in a different table.

For example, in an incident table, you might include a reference field that indicates the user linked to a particular incident. This type of field contributes to a more interconnected data model in ServiceNow, promoting data consistency across diverse tables.

Why is this an issue?

The g_form.getReference() method retrieves the complete record, implying that it fetches the entire object with all field values from the server. This extensive data request can lead to latency in your Client Side action, potentially impacting the performance of the instance, especially in common scenarios where this level of detail is not required.

How do I fix it?

Another approach to fetch data from the server is by utilizing the GlideAjax API. Alternatively, you can achieve this task using a REST API. The following code illustrates how to retrieve, for instance, the manager user:

  • Server side:
1var getUserData = Class.create(); 2getUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, { 3 getUserSomeData: function (user_id, user_field) { 4 user_id = user_id || this.getParameter("sysparm_user_id"); 5 user_field = user_field || this.getParameter("sysparm_user_field"); 6 7 if (!user_id || !user_field) { 8 return ""; 9 } 10 11 var getUser = new GlideRecord("sys_user"); 12 if (getUser.get(user_id)) { 13 // Return the value to Client Side 14 return getUser.getDisplayValue(user_field); 15 } 16 }, 17 type: "getUserData", 18});
  • Client side:
1// GlideAjax receives Script Include name as the first parameter 2var ga = new GlideAjax("getUserData"); 3// sysparm_name is a reserved name, representing the Script Include function to be called 4ga.addParam("sysparm_name", "getUserSomeData"); 5 6// These sysparm_ parameters will be used by the Script Include 7ga.addParam("sysparm_user_id", g_form.getValue("caller_id")); 8ga.addParam("sysparm_user_field", "manager"); 9 10// Calling a function makes this GlideAjax asynchronous 11ga.getXML(getManager); 12 13function getManager(response) { 14 // Response is received as follows 15 var answer = response.responseXML.documentElement.getAttribute("answer"); 16 if (answer) { 17 g_form.addInfoMessage("Caller Manager is " + answer); 18 } else { 19 g_form.addInfoMessage("No Caller Manager found"); 20 } 21}
Do not use client side GlideRecord
Avoid DOM Manipulation

© Copyright 2025. All rights reserved.