GlideRecord insert. Is .initialize() necessary?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2014 04:57 AM
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!
- 66,751 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2017 04:12 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2017 04:23 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2017 04:33 AM
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2017 04:51 AM
Hello Jeet, yes you're correct, once the insert() method is called it will generate the sys_id.
So many methods, so many situations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2017 04:56 AM
Haha Right. That is called ServiceNow loop