top of page

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

ServiceNow Scripting: Understanding GlideRecord.initialize() vs. GlideRecord.newRecord()

Updated: Mar 29

For ServiceNow developers, mastering the GlideRecord object is fundamental to interacting with the platform's database. When it comes to creating new records programmatically, two methods often come into play: initialize() and newRecord(). While both prepare a GlideRecord object for a new insertion, they operate quite differently under the hood. Understanding these distinctions is crucial for writing efficient and predictable ServiceNow scripts. This article will break down the core differences between these two methods, providing clarity on when to use each.


Let's dive into the specifics of GlideRecord.initialize() and GlideRecord.newRecord() to understand their individual behaviors and how they impact your ServiceNow development efforts.


Understanding the Role of GlideRecord in Creating New Entries

Before we differentiate the two methods, it's important to remember that GlideRecord is the primary way to interact with ServiceNow tables through scripting. When you want to add a new record to a table, you first create a GlideRecord object for that table. Then, you use either initialize() or newRecord() to prepare this object for the new data.


GlideRecord.initialize(): Starting with a Clean Slate

The initialize() method, when called on a GlideRecord object, essentially creates an empty record in memory. Think of it as preparing a blank form with no pre-filled values.

  • Creates an Empty Record: It instantiates a new record object without populating any fields with default values.

  • No Immediate Default Values: At the point of calling initialize(), the fields of your new GlideRecord object will be empty. Default values defined in the ServiceNow dictionary for the table are not automatically applied.

  • Population Before Insertion: This method is ideal when you intend to programmatically set all the necessary field values before inserting the record into the database.

Example of GlideRecord.initialize()

JavaScript

var incident = new GlideRecord('incident');
incident.initialize();
gs.info('Opened at (after initialize): ' + incident.opened_at.getDisplayValue());// At this point, incident.opened_at will likely be empty.
incident.short_description = 'Issue reported via script';
incident.caller_id = '6816f79cc0a8000b001cf9b1e14bf7e4'; // Example sys_id
// You would continue to set other fields as needed
incident.insert();

GlideRecord.newRecord(): Defaults and Immediate Identity

In contrast, the newRecord() method takes a more proactive approach when creating a new record.

  • Creates a Record and Sets Defaults: This method not only creates a new GlideRecord object but also immediately populates its fields with the default values defined in the ServiceNow dictionary for the specified table.

  • Assigns a Unique ID: Importantly, GlideRecord.newRecord() assigns a unique system identifier (sys_id) immediately upon calling the method, even though the record is not yet inserted into the database.

  • Leveraging Default Configurations: This method is particularly useful when you want to rely on the system's default field values and only need to set specific fields that differ from the defaults.

Example of GlideRecord.newRecord()

JavaScript

var incident = new GlideRecord('incident');
incident.newRecord();
gs.info('Opened at (after newRecord): ' + incident.opened_at.getDisplayValue());
gs.info('Incident Number (after newRecord): ' + incident.number);
// You'll likely see a default value for opened_at and a new incident number.
incident.short_description = 'Issue reported via script';
incident.caller_id = '6816f79cc0a8000b001cf9b1e14bf7e4'; // Example sys_id
incident.insert();

Key Differences Summarized

Feature

GlideRecord.initialize()

GlideRecord.newRecord()

Default Values

Not applied immediately

Applied immediately

Unique ID (sys_id)

Assigned upon database insertion

Assigned immediately upon calling method

Initial State

Empty record

Record with default values and sys_id

Use Case

Setting all values programmatically

Leveraging default values

Practical Examples and Use Cases

  • Using initialize(): Imagine a scenario where you are integrating data from an external system. You might use initialize() to create a new record and then meticulously populate each field with the data received from the external source, ensuring complete control over the field values.

  • Using newRecord(): Consider a workflow where you want to create a new incident record through a script. Using newRecord() allows the system to automatically set default values for fields like "Urgency" or "Impact" based on your instance's configuration. You would then only need to set the specific details of the incident, such as the short description and caller.


When to Choose Which Method

Generally, GlideRecord.newRecord() is often the preferred method for creating new records in ServiceNow scripts. It aligns well with typical record creation processes where leveraging default values saves development time and ensures consistency.

However, GlideRecord.initialize() is valuable when you need to have complete control over the initial field values and want to set them all programmatically before the record is inserted. This might be the case during complex data integrations or when creating records with very specific, non-default configurations.

It's also worth noting that while initialize() doesn't set default values immediately, they will be applied when the record is inserted into the database if those fields haven't been explicitly set in your script. newRecord(), on the other hand, allows you to access and potentially modify these default values before the insertion occurs.

In some specific and less common scenarios, particularly with certain system tables that have strict mandatory field constraints, using initialize() might be more suitable if newRecord() triggers errors due to unpopulated required fields that don't have default values.


Conclusion

Understanding the subtle yet significant differences between GlideRecord.initialize() and GlideRecord.newRecord() is a key aspect of effective ServiceNow scripting. While initialize() provides a blank slate for complete programmatic control, newRecord() offers the convenience of immediate default values and a unique identifier. By carefully considering your specific scripting requirements and the desired behavior for new record creation, you can choose the method that best fits your needs, leading to more robust and maintainable ServiceNow solutions. As a next step, experiment with both methods in your personal developer instance to observe their behavior firsthand and solidify your understanding. This practical experience will empower you to make informed decisions when scripting new record creation in your ServiceNow environment.

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page