GlideRecord.create() without GlideRecord.initialize()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2022 02:42 PM
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?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2022 11:48 PM
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.