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,754 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2018 03:56 AM
What do these: "name" and "description" present in gr.name and gr.description refer to? Are they field names in the database or what?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2018 04:30 AM
Yes, they are just the fields/columns in the table.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2018 04:09 AM
Hi,
Please check link,
Thanks,
Tripti S.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2019 05:07 AM
Very educational discussion here, thank you everyone for sharing your insights.
Here's one that I'll add.
I had a situation where I needed to assign a pre-defined sys_id to a new record, and was worried that I wouldn't be able to if I used .initialize()
However, I was able to use .initialize() and use my pre-defined sys_id just fine:
var newPostSysid = gs.generateGUID();
var newPost = new GlideRecord('x_tcoa_wallboards_wallboard_post');
newPost.initialize();
newPost.setNewGuidValue(newPostSysid);
newPost.title = "test title";
var theID = newPost.insert();
Hope this was helpful for someone.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 01:14 AM
The gr.initialize() method in ServiceNow is used to initialize a new GlideRecord. Here's what it does: - It sets default values for the fields in the GlideRecord. - It does not retrieve any records from the database. - It is typically used when you want to create a new record. If you skip gr.initialize(), the record will still be created, but any fields that have default values defined in the table schema will not be set to their default values. Instead, they will be null (or their data type's equivalent of null) until you explicitly set a value. Here's a summary: - gr.initialize() initializes a new GlideRecord and sets default values for fields. - It does not retrieve any records from the database. - It is used when creating a new record. - If skipped, fields with default values will not be set to their defaults, but will be null until a value is set. - The record will still be created even if gr.initialize() is skipped.