Creating Records With GlideRecord initialize() and insert()

What Are initialize() and insert()? In ServiceNow scripting, a common requirement is to create new records in a table. While this can be done manually through the UI, using scripts with initialize() and insert() methods …


Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now

What Are initialize() and insert()?

In ServiceNow scripting, a common requirement is to create new records in a table. While this can be done manually through the UI, using scripts with initialize() and insert() methods automates the process, saving time and effort. But what makes scripting such an essential tool for ServiceNow admins and developers?

Scripting for record creation not only increases efficiency but also promotes data integrity by reducing human error. It’s crucial, however, to exercise caution. Misuse of these scripting methods can lead to unintended consequences, like data overwriting or duplication. Always ensure your scripts target the correct tables and fields, and employ good practice like validating data before inserting.

In ServiceNow, initialize() and insert() are methods of the GlideRecord class. The initialize() method prepares a new record for entry into a database table, and the insert() method submits the new record to the database. It’s like preparing a letter (initialize()) and posting it (insert()).

Using initialize() and insert() In A Coding Example

Here’s a real-world scenario. Imagine you need to automate the creation of a new incident record when a specific type of email is received. You might create a script like this:

var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = "New incident from email";
gr.description = "This incident was created automatically from an incoming email.";
gr.insert();

In this example, we’re using initialize() to prepare a new incident record. We then set the ‘short_description’ and ‘description’ fields before using insert() to add the new record to the ‘incident’ table.

Alternatively, you might need to create a new user in the ‘sys_user’ table based on data received from an external API as a JSON object. Here’s an example:

var userData = {
    "first_name": "Jane",
    "last_name": "Doe",
    "email": "jane.doe@example.com"
};

var gr = new GlideRecord('sys_user');
gr.initialize();
gr.first_name = userData.first_name;
gr.last_name = userData.last_name;
gr.email = userData.email;
gr.insert();

In this script, we’re using a JSON object named userData to store the new user’s information. We then use initialize() to prepare a new record in the ‘sys_user’ table and insert() to create the new user.

Can I Leave Out Initialize()?

The initialize() method in ServiceNow is used to initialize a new GlideRecord. When you’re creating a new record, initialize() sets default values for the fields in the record and prepares it to be filled with data before being inserted into the database.

If you leave out initialize(), ServiceNow will still let you create a new GlideRecord and set its fields. The insert() method will create the new record in the database, even if initialize() was not called.

However, the initialize() method can be useful if the table has default values set for certain fields. When initialize() is called, it sets these default values in the new GlideRecord. If initialize() is not called and you don’t manually set these fields, they may not contain the correct default values when the record is inserted.

Therefore, the best practice is to use initialize() when creating a new GlideRecord to ensure that all fields have their correct default values. This can prevent bugs and unexpected behavior in your scripts and applications.

Creating records with initialize() and insert() in ServiceNow scripting can profoundly enhance your workflow by automating routine data entry tasks.

Utilizing JSON objects in your scripts allows you to handle complex, structured data efficiently, making your scripts more dynamic and powerful.

As a ServiceNow admin or developer, mastering these scripting methods will equip you with a valuable toolset to streamline your operations and ensure data integrity.

Remember, the key to effective scripting is understanding your data, knowing your tables and fields, and always testing your scripts thoroughly. Happy scripting!



What Do You Think About This Article?

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x