Amit Gujarathi
Giga Sage
Giga Sage

Mastering GlideRecord Update and Insert with Reference in ServiceNow: A Practical Guide

 

Mastering GlideRecord Update and Insert with Reference in ServiceNow: A Practical Guide

ServiceNow scripting can sometimes feel complex, especially when dealing with related records and reference fields. If you’ve ever found yourself writing multiple scripts just to update or insert related data, this guide is for you. This article walks you through two powerful GlideRecord methods—updateWithReferences and insertWithReferences—that simplify your scripting, save time, and ensure data consistency across tables.

 

Mastering GlideRecord Update and Insert with Reference in ServiceNow: A Practical Guide 

 


 

Understanding GlideRecord and Reference Fields

Before diving into the methods, it’s important to understand what GlideRecord is. GlideRecord is a ServiceNow API that allows you to interact directly with database tables—querying, inserting, updating, and deleting records with ease. Reference fields in ServiceNow link one record to another, such as an incident’s caller referencing a user record.

Traditionally, updating or inserting data in both a record and its referenced record required multiple GlideRecord scripts. The methods covered here let you do both in a single, streamlined script.


 

Update with Reference: One Script, Multiple Tables

What it does: The updateWithReferences() method updates a record and its related reference fields simultaneously. This means you can change an incident’s details and update the caller’s phone number in one go.

Benefits:

  • Reduces scripting complexity by eliminating the need for multiple GlideRecord queries.

  • Ensures data consistency across related tables.

  • Saves development time and effort.

Practical Example: Imagine you want to update an incident’s short description and the caller’s business phone number at the same time. Instead of writing two separate scripts, you can do this:

current.short_description = "This is a new short description";
current.caller_id.phone = "0000000000";  // Update caller's phone number
current.updateWithReferences();

This single script updates both the incident and the caller’s user record seamlessly.


 

Insert with Reference: Creating Related Records Together

What it does: The insertWithReferences() method allows you to insert a new record along with its related reference records in one operation.

Benefits:

  • Simplifies complex insert operations by handling multiple tables at once.

  • Automatically creates related records if they don’t exist.

  • Enhances script readability and maintainability.

Practical Example: Suppose you want to create a new incident and simultaneously create a new user as the caller. Using insertWithReferences(), you can initialize the incident, set the caller’s details, and insert both records together:

var inc = new GlideRecord('incident');
inc.initialize();
inc.short_description = "This is a fresh new incident";
inc.caller_id.setDisplayValue("Divya Gujarati");
inc.caller_id.first_name = "Divya";
inc.caller_id.email = "divya@gmail.com";
inc.insertWithReferences();

This script creates both the incident and the user record in one smooth operation.


 

Conclusion: Streamline Your ServiceNow Development

By mastering updateWithReferences() and insertWithReferences(), you can significantly reduce the complexity of your ServiceNow scripts. These methods help maintain data integrity across related tables and save valuable development time. Whether updating an incident and its caller’s details or inserting new records with related references, these GlideRecord functions are essential tools for any ServiceNow developer.

 

Did you find this guide helpful? Have you tried using these GlideRecord methods in your projects? Share your experiences or questions in the comments below—we’d love to hear from you! For more practical ServiceNow tips and tutorials, stay tuned and keep scripting smarter.

Version history
Last update:
‎05-06-2025 10:45 AM
Updated by:
Contributors