top of page

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

Navigating GlideRecord Arrays in ServiceNow: Resolving Common Data Duplication

Updated: Mar 29


ServiceNow developers frequently encounter the need to manipulate data retrieved from database tables through GlideRecord queries. One common task involves storing these records in an array for subsequent processing, particularly within server-side scripts. However, a recurring issue arises when attempting to push GlideRecord objects directly into an array within a loop: the array ends up containing multiple references to the same, typically the last, record. This article addresses this problem, providing a clear understanding of its cause and offering effective solutions for ServiceNow developers.


The core issue stems from how JavaScript handles object references. When a GlideRecord object is directly pushed into an array within a loop, each array element does not receive a distinct copy of the record at that iteration. Instead, they all retain a reference to the same GlideRecord object, which is continuously updated as the loop progresses. Consequently, by the loop's end, all array elements point to the final state of the GlideRecord object.


To resolve this, developers must create independent JavaScript objects representing the desired data from each GlideRecord. This can be achieved by extracting specific field values and assigning them to properties of a new object, which is then pushed into the array.


Troubleshooting Solutions:


  1. Creating JavaScript Objects:

    • Initialize an empty array.

    • Execute the GlideRecord query.

    • Within the while(gr.next()) loop:

      • Create a new JavaScript object.

      • Assign the required field values from the current GlideRecord to the object's properties.

      • Push the newly created object into the array.

    • This method ensures that each array element holds a distinct object containing the relevant data from each GlideRecord.


  2. Cloning GlideRecord Objects (Advanced):

    • For scenarios where a full copy of the GlideRecord is necessary, a cloning function can be implemented.

    • This function creates a new GlideRecord of the same type and copies all field values from the original.

    • This method is more resource-intensive and should be used when a complete GlideRecord clone is essential.


Practical Examples and Use Cases:


  • Generating Reports: Constructing an array of incident data to format and display in a custom report.

  • Data Transformation: Gathering data from multiple GlideRecords to create a structured dataset for integration with external systems.

  • Client-Side Data Transfer: Serializing the array as JSON and transmitting it to the client for dynamic rendering in a user interface.


Alternative Solutions:

  • If only specific field values are needed, consider constructing a simple array of strings or numbers instead of objects.

  • For complex data manipulations, explore using script includes to encapsulate logic and improve reusability.


Effectively managing GlideRecord arrays is crucial for ServiceNow development. By understanding the underlying issue of object references and implementing the provided solutions, developers can prevent data duplication and ensure accurate data processing. When constructing arrays from GlideRecord queries, always create new javascript objects that contain the data you need from each record. When full copies of the glide record are needed, create a glide record cloning function. Moving forward, developers should prioritize creating new Javascript objects from GlideRecord data when pushing data into arrays. This will ensure they have distinct data in each array index.

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page