What is a GlideRecord?
A GlideRecord is a JavaScript object that represents a record in a ServiceNow database. It is used to access and manipulate data in the database. GlideRecords can be used to query data, insert data, update data, and delete data. GlideRecords can be used in both server-side and client-side scripts. They are a powerful tool for accessing and manipulating data in the ServiceNow database.
Why is this an issue?
When you use a GlideRecord instance to get a value from a database, you are actually getting a reference to a GlideElement instance. This means that you are getting more data than you might expect, including the GlideElement object itself, as well as any nested objects, which contains more data than primitive values (integer, string, float, etc.). This can be a problem for some reasons:
Debugging problems: When you use a GlideRecord instance, you are getting more data than you might expect. This can make debugging difficult, because you may not be able to access the value you need directly.
Performance problems: The GlideElement object is a JavaScript object, and JavaScript objects can be slow to access. If you are accessing a GlideElement object frequently, it can slow down your code.
One example of code that is commonly used but not correct is the following:
1 2 3 4 5 6 7 8 9
// current is representing the current record in the Task table. // Getting the sys_id of the current record. var task_sys_id = current.sys_id; // OR // Getting the sys_id of a record in the Task table. var some_record_id = gr.sys_id;
How do I fix it?
To fix these potential issues related to GlideRecord and follow best practices in ServiceNow, you can update the wrong code with the following two methods:
- Use the
getUniqueValue()
method. This method will return the value of the specified field, without returning any additional data:
1 2 3 4 5 6 7
// On Server Side // Gets the unique value of the `sys_id` field from the current record. var some_record_id = current.getUniqueValue(); // On Client Side // Gets the unique value of the `sys_id` field from the current form. var some_record_id = g_form.getUniqueValue();
2. Use the getValue()
method. This method will also return the value of the specified field, but it will also return the type of the value:
1 2 3
// Using getValue() method // Gets the value of the `sys_id` field from the current record. var currentTask = current.getValue("sys_id");