The Zurich release has arrived! Interested in new features and functionalities? Click here for more

What is the difference Between gr.initialize() and gr.newRecord() ?

SohamTipnis
Tera Contributor

Can somebody clear my doubt on this? and  Which is more preferable?

 

Thanks & Regards,

Soham 

3 ACCEPTED SOLUTIONS

Shashank_Jain
Kilo Sage

@SohamTipnis ,

 

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!

 

 

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain

View solution in original post

Shruti D
Kilo Sage

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

View solution in original post

sumityadav8
Tera Contributor

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

var gr = new GlideRecord("incident");
gr.initialize();
gs.print(gr.getValue('category'));
gr.setValue('caller_id','abedbd9a534722101ade32a0a0490ee7');
gr.setValue('short_description','Hii testing now');
gr.insert();
Run this Query you will get null output, because gr.initiliaze() is creating completely blank records in all fields
While gr.newRecord() creates a new blank record where default values are already populated in
var gr = new GlideRecord("incident");
gr.newRecord();
gs.print(gr.getValue('category'));
gr.setValue('caller_id','abedbd9a534722101ade32a0a0490ee7');
gr.setValue('short_description','Hii testing now');
gr.insert();
here You will get Output as 'Inquiry' which is the default value of category field of incident table
As per Performance is Concern, it's better to use newRecord();
Warm Regards
Sumit Y

View solution in original post

8 REPLIES 8

debendudas
Mega Sage

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👍

That's perfectly explained.😀

Thank you so much @debendudas 

Shashank_Jain
Kilo Sage

@SohamTipnis ,

 

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!

 

 

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain

Thank you for the great answer @Shashank_Jain 😀