GROW

code

Never dot-walk to sys_id

This issue is found automatically by CODA.
Time to fix: 01h

What is Dot Walking?

Dot-walking in ServiceNow is a simplified way of accessing and retrieving data within the platform. It enables users to conveniently access fields and values from related tables using a straightforward syntax. When the current table contains a reference to another table, dot-walking allows you to access any field on the referenced table directly:

1
2
3
4
5
6
// The 'current' object represents the Task table within a Business Rule.
// Getting the record's sys_id and store it in the 'task_sys_id' variable.
var task_sys_id = current.sys_id;

// The 'current.assigned_to.sys_id' references the sys_id of the user assigned to the task.
var assigned_to = current.assigned_to.sys_id;

Why is this an issue?

Utilizing dot-walking to access data from related tables in ServiceNow introduces potential performance challenges in data handling. This approach can lead to concerns due to the following reasons:

  • Increased SQL Queries: Dot-walking triggers additional SQL queries behind the scenes to fetch the requested data, potentially impacting performance by introducing more database interactions.

  • GlideRecord Complexity: The use of dot walking with GlideRecord instances returns references to GlideElement instances instead of the expected primitive values. This complexity in data structures can lead to challenges in understanding and managing the retrieved data.

  • Behavior Differences: The distinction between references and primitives in JavaScript can result in unexpected behavior, making it challenging to troubleshoot and debug issues related to data retrieval.

How do I fix it?

To address the performance and data handling concerns associated with dot-walking in ServiceNow, consider using the getValue() method. This method allows you to retrieve specific field values without triggering additional database queries:

1
2
3
4
5
6
7
8
9
// Getting the sys_id of the current record without querying the database
var currentTask = current.getUniqueValue();

// For a reference field, retrieving the sys_id without additional database queries
var assigned_to_id = current.getValue("assigned_to");

// For a reference field at multiple levels, accessing the sys_id without querying the database
// Note: For optimal performance, use alternatives like getValue() for dot-walking within a chain of fewer than three levels
var assigned_company = current.assigned_to.company.getValue();

It is important to note that the getValue() method is applicable in global applications. In scenarios where you are working with scoped applications the toString() method can be considered as an alternative for retrieving specific field values without triggering additional database queries.

If you need to obtain the sys_id of the current record, refer to the Use gr.getUniqueValue() article.