The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

What's the difference between GlideRecord.initialize() and GlideRecord.newRecord()?

peterraeves
Mega Guru

Basically what the title says. What's the difference between those two functions?

1 ACCEPTED SOLUTION

Chuck Tomasi
ServiceNow Employee
ServiceNow Employee

Hi Peter,

 

Per the wiki...

 

  • initialize(): Creates an empty record suitable for population before an insert.
  • newRecord(); Creates a GlideRecord, set the default values for the fields and assign a unique id to the record.

Try these two in scripts background and you'll see that initialize gives no value to opened_at, whereas newRecord does.

 

var inc = new GlideRecord('incident');
inc.initialize();
gs.print(inc.opened_at.getDisplayValue());

 

var inc = new GlideRecord('incident');
inc.newRecord();
gs.print(inc.opened_at.getDisplayValue());

 

I have always trusted newRecord more since learning about this a few years ago.

 

GlideRecord - ServiceNow Wiki

 

View solution in original post

18 REPLIES 18

Chuck Tomasi
ServiceNow Employee
ServiceNow Employee

Hi Peter,

 

Per the wiki...

 

  • initialize(): Creates an empty record suitable for population before an insert.
  • newRecord(); Creates a GlideRecord, set the default values for the fields and assign a unique id to the record.

Try these two in scripts background and you'll see that initialize gives no value to opened_at, whereas newRecord does.

 

var inc = new GlideRecord('incident');
inc.initialize();
gs.print(inc.opened_at.getDisplayValue());

 

var inc = new GlideRecord('incident');
inc.newRecord();
gs.print(inc.opened_at.getDisplayValue());

 

I have always trusted newRecord more since learning about this a few years ago.

 

GlideRecord - ServiceNow Wiki

 

Hmm, never noticed that. I outputted all variables and it seems that you are correct that inc.initialize() just empties all fields (and puts all default values in it during insert), while inc.newRecord() actually puts the default values in them when called. So it would indeed make more sense to use newRecord instead of initialize. To be fair, it doesn't even make sense to even use initialize after a "new GlideRecord" as all fields are empty at that point anyway.



Thanks for your fast reply!


You are welcome Peter. I had the same thought when I discovered this. "why would anyone use initialize?", yet everyone does under the assumption that it does what newRecord() does.


This is definitely going in a future video at some point. I'll be sure to cite your question Peter!