Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

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

yes because update() insert a new record as well.

saschawildg
ServiceNow Employee

Here is an additional observation:

For some classes (e.g. sys_db_table_view_field) using newRecord() will even throw errors since some required fields are not populated.

With this in mind, I would now always prefer initialize() over newRecord().  

Not applicable

@harleyl @Chuck Tomasi Don't you need to use insert() rather than update() after you use newRecord() or initialize()?

Or if not, why is there even an insert() method?  Thanks.

var grRecord = new GlideRecord('your_table');

grRecord.initialize();

grRecord.some_field = 'some value';

grRecord.insert();