GlideRecord.create() without GlideRecord.initialize()

Roy Verrips
Tera Expert

As per the documentation, one needs to initialize() before assigning values to later insert() a new Gliderecord, i.e. 

var now_GR = new GlideRecord('to_do');
now_GR.initialize(); 
now_GR.name = 'first to do item'; 
now_GR.description = 'learn about GlideRecord'; 
now_GR.insert();

However I've found things work fine if I exclude the initialize(), i.e. 

var now_GR = new GlideRecord('to_do');
now_GR.name = 'second to do item'; 
now_GR.description = 'I did not initialize this, but still works the same'; 
now_GR.insert();

Is there something I'm missing in the need to initialize the GlideRecord or is this no longer needed and can be omitted? 

1 REPLY 1

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Roy,

initialize() isn't required before executing .insert(). .initialize() will initialize all the values. If initialize() is not used and .insert() is used in a loop, fields that was previously set may be set instead of the field being empty.

e.g. The script below will insert 2 records with both record's u_comments field value set to 'test'.

var gr = new GlideRecord('<table>');
gr.u_comments= 'test';
gr.insert();

var gd = new GlideDate();
gr.u_end_date = gd.getDisplayValue();
gr.insert();

Following script will only have 1 record with u_comments field set to 'test'.

var gr = new GlideRecord('<table>');
gr.initialize();
gr.u_comments= 'test';
gr.insert();

gr.initialize();
var gd = new GlideDate();
gr.u_end_date = gd.getDisplayValue();
gr.insert();

FYI, .newRecord() may be used instead too. .newRecord() will set the default values and assign a unique ID.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/c_GlideRecordAPI#r_Gl...