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.

GlideRecord insert. Is .initialize() necessary?

Kyryl Petruk1
Tera Expert

Hi Everyone,

 

I've just been curious.

Wiki says this is a correct way to insert a record in a table using GlideRecord:

http://wiki.servicenow.com/index.php?title=GlideRecord#Insert_Methods

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

 

But what actually gr.initialize() does?

What happens if I skip it? Will the record be created differently or simply slower?

On practice I don't see any difference, record is created properly.

 

Will appreciate your comments!

Thanks!

29 REPLIES 29

Jeet
Tera Expert


gr.initalize(); // Its always going to create new record with empty fields and set unique no (sys_id) for current record


Creates an empty record suitable for population before an insert.


public void initialize()


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

Other side you can see :



newRecord();


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




Regards,


Jeet.


Jitendra,



From the above experiment, it looks like initialize() does not grab a new unique sys id as it's showing as undefined on instantiating and false on initialize(). It looks like the only way a new sys id gets generated is using the newRecord() method.


Hi Martin,



What I know from the research that once you call the gr.insert() method it is going to generate and returns a unique sys_id.


Mark: If I am wrong somewhere




Regards,


Jeet,


Hello Jeet, yes you're correct, once the insert() method is called it will generate the sys_id.



So many methods, so many situations


Haha Right. That is called ServiceNow loop