What is the specific use of gr.initialize()?

Pranshu3
Tera Expert

Hi,

I saw a OOB Email Inbound Action "Create Incident'' in my developer instance with below script:

current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;
current.insert();

But when I see the ServiceNow docs for inserting new records in table, I see:

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

So, for current GlideRecord, is there no need for initialize function?
Then why is gr.initialize() specifically used?

Thanks,
Pranshu

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

To explain a bit more about this, in your first example, that is in an Inbound Action with the action type set to "record action", so that is essentially setting the stage for the new record being inserted already.

In the second example, this is from a glidequery and the system doesn't know what you're about to do...so you're telling it hey...I'd like to create a new record in this table, please give me a blank canvas and I'll paint the picture then insert.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

6 REPLIES 6

Chuck Tomasi
Tera Patron

For what it's worth, I highly recommend looking in to using "newRecord()". While initialize() creates a "space", newRecord() adds to that and sets default values. Goran Lundqvist did a good video on the differences.

Thanks Chuck, newRecord() in GlideRecord was a new learning for me!