Anshu_Anand_
Kilo Sage

As per the servicenow docs newRecord() method creates a new record just as gr.initialize() .

newRecord() Creates a GlideRecord, set the default values for the fields and assign a unique id to the record.

Here the insert() and update() method both work when inserting the data. 

//using insert()

var gr =new GlideRecord("incident");
gr.newRecord();
gr.caller_id='a8f98bb0eb32010045e1a5115206fe3a';
gr.short_description='demoinsert';
gr.insert(); 

//using update()

var gr =new GlideRecord("incident");
gr.newRecord();
gr.caller_id='a8f98bb0eb32010045e1a5115206fe3a';
gr.short_description='demoupdate';
gr.update();

So i was thinking which method is best since both creates a new record. I know maximum people will go for insert .

My initial thought was also Insert would be faster because in case of update you need to first search for the record that you are going to update and then perform the update .

Then i experimented with creation of some incidents via both insert() and update() method.

I tried to create 100,200,300..1000,5000...10000 incidents via both methods and was surprised by the script transaction process time. Here is the script and initial data :-

//Insert() method

var gr =new GlideRecord("incident");
for(i=1;i<=n;i++) // n = 100,200,300..1000,2000,3000.....10000
{
gr.newRecord();
gr.caller_id='a8f98bb0eb32010045e1a5115206fe3a';
gr.short_description='demo';
gr.insert();
}

 

//update() method

var gr =new GlideRecord("incident");
for(i=1;i<=n;i++) // n = 100,200,300..1000,2000,3000.....10000
{
gr.newRecord();
gr.caller_id='a8f98bb0eb32010045e1a5115206fe3a';
gr.short_description='dasda';
gr.update();
}

 

Comparison:-

find_real_file.png

 

As you can see above initially there is not much difference when number of records created is less. There is an equal win-win situation. Also surprised that update() method is faster sometimes.

But as the number of records increases, there is huge transaction processing time gap increasing with every multiples of N  .

Because update() method tries to find that record which it needs to be updated, its processing time increases with N. Insert just commit to the server.

So ultimately insert() method wins here when comes to create and can be chosen for best method when using newRecord() .

 

Please comment if there is anything needed to be added or any kind of feedback will help .

If its useful , please mark it as helpful

 

 

 

 

 

 

 

Comments
Kingstan M
Kilo Sage

Very good information - Thanks!

GhatakNow
Tera Contributor

This is A very good use case to test, and its no wonder that insert would perform better in a long run. However, I am interested to know if you want to use transaction to run a update statement. And how glidetransaction could impact the results.

Best,

Anirban

Version history
Last update:
‎02-26-2022 01:54 AM
Updated by: