Simplifying Data Management: Scoped GlideRecord's updateWithReferences() Method in ServiceNow

Mohit_Gupta
Tera Guru
Tera Guru

ServiceNow developers often encounter scenarios where updating a record's fields necessitates updating associated records as well. Scoped GlideRecord's updateWithReferences() method addresses this need by enabling atomic updates across related records within a specific scope. In this article, we'll explore the practical usage of Scoped GlideRecord's updateWithReferences() method through an example.

 

Understanding Scoped GlideRecord's updateWithReferences() Method

The updateWithReferences() method in Scoped GlideRecord allows for the seamless updating of a record and its associated records in a single operation. This method ensures data consistency and integrity by executing updates atomically, simplifying data management within ServiceNow applications.

 

Example: Updating Caller ID Information and Associated Incidents 

 

Below example capture from ServiceNow Documentation

 

// Update Caller ID Information and Associated Incidents

(function () {
// Retrieve incident record
var inc = new GlideRecord('incident');
var inc_sys_id = 'INC001234'; // Replace with actual incident sys_id
inc.get(inc_sys_id);

// Update caller ID information
inc.caller_id.first_name = 'John';
inc.caller_id.last_name = 'Doe';

// Perform atomic update across associated records
inc.updateWithReferences();
})();

 

In this example:

  1. We create a new Scoped GlideRecord instance for the 'incident' table and retrieve an existing incident record using its sys_id.
  2. We update the caller ID information associated with the incident record by modifying the first_name and last_name fields.
  3. We execute the updateWithReferences() method on the incident record, which ensures that any associated records referencing the incident (such as tasks or related incidents or user record) are also updated atomically.

Benefits of updateWithReferences() Method

  • Data Consistency: Updates across related records occur atomically, maintaining data consistency.

  • Efficiency: Multiple related records can be updated in a single operation, reducing the need for multiple update calls.

  • Data Integrity: Updates are executed together, minimizing the risk of data inconsistencies and ensuring system reliability.

Conclusion

Scoped GlideRecord's updateWithReferences() method simplifies data management within ServiceNow applications by facilitating atomic updates across related records. In our example, updating caller ID information and associated incidents demonstrates how this method streamlines the updating process while ensuring data consistency and integrity. Incorporate updateWithReferences() into your ServiceNow development workflows to optimize data management processes and enhance the reliability of your applications.

 

0 REPLIES 0