- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Can somebody clear my doubt on this? and Which is more preferable?
Thanks & Regards,
Soham
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
gr.initialize()
Used after you already created a GlideRecord object and selected a table.
gr.newRecord()
Creates a new empty record for the table in one step.
Internally, it calls initialize(), but also does some extra setup:
It executes any business rules or default values defined for when = new or Display Business Rules.
In simple words if I have to explain this to you -
Use initialize() when you just want a blank record quickly (no defaults).
Use newRecord() when you want a record with defaults and business rules applied, just like creating it from the UI.
newRecord() behaves like clicking "New" on the Incident form.
Any default values (like Priority = 3, State = New) or "on new" Business Rules will run.
So the record gets created with those defaults already set.
Hope it helps!
Shashank Jain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago - last edited 4 weeks ago
Hello @SohamTipnis,
- gr.initialize()
Purpose: Prepares a new record for insertion.
What it does: makes a new empty record with no values filled in. It resets everything so you can start fresh.
Use: When you want to insert a new record into a table.
Example:
var incidentGR = new GlideRecord('incident');
incidentGR.initialize(); // starts a new empty record
incidentGR.short_description = 'Test incident';
incidentGR.insert();
This is used to create a new record manually.
- gr.newRecord()
Purpose: Similar to initialize(), but also pre-populates default values, including ones from the dictionary, default values, or Business Rules.
What it does: Creates a new record and sets default values based on the table's dictionary and from client scripts or UI policies (on the server).
Use: When you want a new record with default field values already set.
Example:
var incidentGR = new GlideRecord('incident');
incidentGR.newRecord(); // new record with default values
gs.info(incidentGR.category); // might show a default value
So in short,
gr.initialize(): Creates a completely empty new record with no default values set.
gr.newRecord(): Creates a new record and automatically fills in the default values defined in the system.
Please Mark Correct ✔️ if this solves your query and also mark Helpful 👍 if you find my response worthy based on the impact.
Regards,
Shruti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @SohamTipnis ,
Whenever you are using gr.initialize() it creates a blank record in memory, here default values are not populated in the blank record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago - last edited 4 weeks ago
Hi @SohamTipnis ,
gr.initialize() creates a blank record in memory, while gr.newRecord() does the same but also sets system-generated default values for fields like sys_id, opened_at, and returns the unique ID (sys_id), making it suitable for operations that need these default values before the record is actually inserted into the database.
Here is a good video which you can refer for more information: https://www.servicenow.com/community/developer-articles/servicenow-initialize-vs-newrecord-serviceno...
If this solution helps you then, mark it as accepted solution ✔️ and give thumbs up👍!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
That's perfectly explained.😀
Thank you so much @debendudas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
gr.initialize()
Used after you already created a GlideRecord object and selected a table.
gr.newRecord()
Creates a new empty record for the table in one step.
Internally, it calls initialize(), but also does some extra setup:
It executes any business rules or default values defined for when = new or Display Business Rules.
In simple words if I have to explain this to you -
Use initialize() when you just want a blank record quickly (no defaults).
Use newRecord() when you want a record with defaults and business rules applied, just like creating it from the UI.
newRecord() behaves like clicking "New" on the Incident form.
Any default values (like Priority = 3, State = New) or "on new" Business Rules will run.
So the record gets created with those defaults already set.
Hope it helps!
Shashank Jain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Thank you for the great answer @Shashank_Jain 😀