- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2016 04:11 AM
Basically what the title says. What's the difference between those two functions?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2016 04:15 AM - edited ‎06-29-2023 07:27 AM
Hi Peter,
Per the wiki...
- initialize(): Creates an empty record suitable for population before an insert.
- newRecord(); Creates a GlideRecord, set the default values for the fields and assign a unique id to the record.
Try these two in scripts background and you'll see that initialize gives no value to opened_at, whereas newRecord does.
var inc = new GlideRecord('incident');
inc.initialize();
gs.print(inc.opened_at.getDisplayValue());
var inc = new GlideRecord('incident');
inc.newRecord();
gs.print(inc.opened_at.getDisplayValue());
I have always trusted newRecord more since learning about this a few years ago.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2018 12:09 PM
Thank you for the elaborated response Chuck! It was helpful.
Regards,
Hitesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2023 02:20 AM
Hey Chuck ,
Thanks for your valuable insights , may i know conclusion that which we should use while creating records through scripts and why that only and why not another?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2020 12:09 PM
I played around with this today because I wanted to verify default values were NOT set when using initialize. Well - that is not a true statement...once update() is done, defaults ARE set so I'm not as concerned with using initialize() any longer.
initialize(): Creates an empty record suitable for population before an insert. Upon update, default values are set unless otherwise populated in the GlideRecord.
newRecord(); Creates a GlideRecord, set the default values for the fields and assign a unique id to the record.
If you run the below code, you will see the default value for urgency - is you uncomment the update, you will see the urgency is defaulted using initialize or newRecord.
var inc = new GlideRecord('incident');
inc.initialize();
//inc.update();
gs.print('Opened at: ' + inc.opened_at.getDisplayValue() + ' Urgency:' + inc.urgency);
var inc = new GlideRecord('incident');
inc.newRecord();
//inc.update();
gs.print('Opened at: ' + inc.opened_at.getDisplayValue() + ' Urgency:' + inc.urgency);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2020 06:59 AM
Yes, you will get the default values from initialize() when the record is submitted. The point of the difference is that you will NOT get them before update. newRecord() populates the values right away and you can use them BEFORE you submit.
Example:
var incGr = new GlideRecord('incident');
incGr.initialize();
gs.info(incGr.number); // nothing displayed
versus
var incGr = new GlideRecord('incident');
incGr.newRecord();
gs.info(incGr.number); // displays the next number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2020 02:23 PM
Yes - that is the point I was making. What was unclear is what I highlighted:
initialize(): Creates an empty record suitable for population before an insert. Upon update, default values are set unless otherwise populated in the GlideRecord.
Thanks for the follow up!
