Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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
Tera Patron

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

Thank you for the elaborated response Chuck! It was helpful.

 

Regards,

Hitesh

Hey Chuck ,

 

Thanks for your valuable insights , may i know conclusion that which we should use while creating records through scripts and why that only and why not another?

harleyl
Giga Contributor

I played around with this today because I wanted to verify default values were NOT set when using initialize.  Well - that is not a true statement...once update() is done, defaults ARE set so I'm not as concerned with using initialize() any longer.

initialize(): Creates an empty record suitable for population before an insert.  Upon update, default values are set unless otherwise populated in the GlideRecord.

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

If you run the below code, you will see the default value for urgency - is you uncomment the update, you will see the urgency is defaulted using initialize or newRecord.

var inc = new GlideRecord('incident');
inc.initialize();
//inc.update();
gs.print('Opened at: ' + inc.opened_at.getDisplayValue() + ' Urgency:' + inc.urgency);

var inc = new GlideRecord('incident');
inc.newRecord();
//inc.update();
gs.print('Opened at: ' + inc.opened_at.getDisplayValue() + ' Urgency:' + inc.urgency);

Yes, you will get the default values from initialize() when the record is submitted. The point of the difference is that you will NOT get them before update. newRecord() populates the values right away and you can use them BEFORE you submit.

Example:

var incGr = new GlideRecord('incident');
incGr.initialize();
gs.info(incGr.number); // nothing displayed

 

versus

 

var incGr = new GlideRecord('incident');
incGr.newRecord();
gs.info(incGr.number); // displays the next number

Yes - that is the point I was making.  What was unclear is what I highlighted:

initialize(): Creates an empty record suitable for population before an insert.  Upon update, default values are set unless otherwise populated in the GlideRecord.

Thanks for the follow up!