You are currently viewing insertWithReferences() vs insert() ServiceNow

insertWithReferences() vs insert() ServiceNow

Hello Spectaculars,

Welcome to new article of ServiceNow Spectaculars.

In this article we understand the basic difference between two functions of ServiceNow GlideRecord method: insertWithReferences() vs insert().

What is insert() function?

In ServiceNow, the insert() function is a GlideRecord method used to insert a new record into a table with field values necessary. GlideRecord is a class in ServiceNow used for database operations such as querying, inserting, updating, and deleting records.

Here is an example of using the insert() function to insert a new record into the incident table in ServiceNow:-

// Create a new incident record

var incident = new GlideRecord('incident');
incident.setValue('short_description', 'New incident');
incident.setValue('description', 'This is a new incident record');
var incidentSysId = incident.insert();
gs.print(incidentSysId);
Output:-
*** Script: 21b012f5071221105ff1f9fc7c1ed037

In the above example, a new GlideRecord object is created for the incident table, and values are set for the short_description and description fields of the new incident record.

What is insertWithReferences()?

In ServiceNow, insertWithReferences() is a GlideRecord method used to insert a new record into a table along with any related records that have reference fields to the new record. This method automatically inserts the new record and any related records in the correct order based on their references to each other.

For example, suppose you have a parent table called incident and a child table called incident_task, where incident_task has a reference field to incident. To insert a new incident record along with a new incident_task record that references the new incident record, you can use the insertWithReferences() method.

// Create a new incident record

var incident = new GlideRecord('incident');
incident.setValue('short_description', 'New incident');
incident.setValue('description', 'This is a new incident record');
var incidentSysId = incident.insert();
// Create a new incident_task record that references the new incident record

var incidentTask = new GlideRecord('incident_task');
incidentTask.setValue('short_description', 'New incident task');
incidentTask.setValue('description', 'This is a new incident task record');
incidentTask.setValue('incident', incidentSysId); // Set the incident reference field to the new incident record
incidentTask.insertWithReferences();

insertWithReferences() Vs insert()

Here is a comparison table:

insertWithReferences()insert()
Main PurposeInsert a new record along with related recordsInsert a new record
Related RecordsInserts related records with reference fields to new recordDoes not insert related records
SyntaxinsertWithReferences();insert();
ReturnsReturns the sys_id of the newly inserted recordReturns the sys_id of the newly inserted record
UsageUsed when inserting records with related records, such as in the case of a parent-child relationshipUsed when inserting records that do not have any related records
PerformanceSlower performance compared to insert() due to the additional processing required to insert related recordsFaster performance compared to insertWithReferences() due to the lack of additional processing required to insert related records
end footer

Leave a Reply