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,742 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2014 05:42 AM
From what I've seen and used, I'd say it always has to be there (although I've never tried not using it on an insert). On occasion I see code from people using .initialize() on a simple query, too, and not just inserts and things appear to work okay.
I swear a couple months ago the wiki said initialize() was used to get the record ready for creation but now I can't find that area in the Query article. I want to say it's necessary but sadly can't find anything that explains what it's actually doing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2014 05:08 PM
.initialize() forces a clean record for the insert.
The following thread is a good example of the effect of .initialize() Glide Record not inserting record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2014 03:35 AM
Thank you for the useful thread link! It gives a better understanding of what .initialize() actually does.
What I'm still curious about is pros and cons of using .initialize() on a fresh GlideRecord like it is recommended on the wiki.
For example, if I already have insert of a GlideRecord without .initialize() in 10 scripts - do I need to rewrite them?
Thanks!
Kyryl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2014 02:19 PM
i would actually go back and correct them.. without the initialize you might get unreliable results in the future.. the initialize sets the correct format for the fields and ensures they are all set to the default values correctly...
so while your scripts may be working fine.. they might crash in the future leaving you with unreliable results.