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,750 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2016 03:24 PM
Could you provide a screenshot or evidence of your observations, because when I run the following script:
var gr = new GlideRecord('incident');
gs.print(gr.sys_id);
gr.initialize();
gs.print(gr.sys_id);
gr.newRecord();
gs.print(gr.sys_id);
I get the following output:
[0:00:01.144] Script completed in scope global: script
*** Script: undefined
*** Script:
*** Script: fa5bd3800fec2600263387ece1050e78
Clearly indicating that initialize only empties fields, but newRecord sets the default values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2016 03:39 PM
I could be mistaken - it's been a while back, under Eureka. Something may have changed since, or I might not be remembering correctly. Unfortunately I don't have time to go back and try to reproduce it to get further clarification, so in the interest of time we'll assume I have no idea what I'm talking about.
I will say, why would you need to empty fields on a new record? Without a concept of what the current record is, what record is it clearing?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2016 09:27 AM
I see what you mean Peter, I thought I'd take it a bit further and introduce the 'isNewRecord()' in the equation.
So on the following:
var gr = new GlideRecord('incident');
gs.print(gr.sys_id);
gs.print(gr.isNewRecord());
gr.initialize();
gs.print(gr.sys_id);
gs.print(gr.isNewRecord());
gr.newRecord();
gs.print(gr.sys_id);
gs.print(gr.isNewRecord());
Output:
*** Script: undefined
*** Script: false
*** Script:
*** Script: false
*** Script: 3dfe68f0db00320004607befbf96196e
*** Script: true
I was surprised to see the isNewRecord method return false on both instantiating the GlideRecord and on initialize(). So I suppose only really use initialize() on querying etc, on creating new records then the newRecord() method should be used eh.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2016 04:38 PM
I was surprised by the isNewRecord result as well. I feel the documentation doesn't accurately describe it's behavior:
isNewRecord()
Checks if the current record is a new record that has not yet been inserted into the database.
https://developer.servicenow.com/app.do#!/api_doc?v=fuji&id=r_ScopedGlideRecordIsNewRecord
I feel like it should read
Checks if newRecord() has been previously called OR if the record has been previously inserted into the database.
I agree that newRecord should probably be called for creating new records. I'm not convinced that initialize has a practical use unless you don't want to populate a new record with default values.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2016 05:50 AM
Indeed Paul, I never use initialize() after instantiating the GlideRecord class because I read somewhere that it is invoked when you first call it anyway, the only time I have used the initialize() method is if I wish to create multiple records in a loop. I know someone said on this thread for some reason to not use it in a loop but I have and have never had any issues, but I'm always open to learning and perfecting my code.
I'm assuming you'd use different methods depending on the situation (i.e. if you want the sys id prior to inserting).