Gr next servicenow
GlideRecord Query Cheat Sheet
I doubt if there’s a single concept in Service-now that is more valuable to understand than how to use GlideRecord methods to query, insert, update, and delete records in your system. These methods have a wide variety of uses and are found at the heart of many of the business rules, UI actions, and scheduled job scripts that are essential to tie together your organization’s processes in your Service-now instance.
While the content of this post isn’t new information (additional examples can be found on the Service-now wiki), my aim is to provide a single page of information containing some common examples of these methods as a reference. This is an excellent page to keep bookmarked!
Query
Can also be used in Client scripts and UI policies.
A standard GlideRecord query follows this format.
var gr = new GlideRecord('incident'); //Indicate the table to query from //The 'addQuery' line allows you to restrict the query to the field/value pairs specified (optional) //gr.addQuery('active', true); gr.query(); //Execute the query while (gr.next()) { //While the recordset contains records, iterate tCodeCreative Logo, click to navigate home
In a troubleshooting conversation with a couple of my teammates, I realized that I take GlideRecord (and its partner GlideAggregate) for granted. When you make that simple gr.query() call, what happens? What SQL does it run? What does the real underlying table structure look like? Are there indexes? Are additional queries run when you dot walk? Does any of this hit a cache? How does the cache work? Where are the optimizations under the hood?
But when performance issues start to pile up… well, what you don’t know could be killing your instance.
But when performance issues start to pile up... well, what you don't know could be killing your instance.
The truth is, most of us have no idea what happens and most of the time that is really awesome! If the queries run fast and returns accurate data we usually don’t care how ServiceNow made it happen.
But when performance issues start to pile up on that scripted REST endpoint and all you’re doing is generating JSON from GlideRecords… well, what you don’t know could be killing yo
GlideRecord Cheat Sheet
Below is a list of commonly used GlideRecord code that you can come help to daily while writing scripts in ServiceNow.
The examples are in no specific order - so just ctrl+f or cmd+f and search to find what you need!
Query
Basic GlideRecord query
Get (sys_id)
Get a single GlideRecord with sys_id. A great way to restore a single record when you know the sys_id of that record.
OR
The standard ‘addQuery’ parameter acts like an ‘and’ condition in your query. This example shows how you can add ‘or’ conditions to your query.
OR (the plain way)
In addition to the example above this, you can also chain your ‘OR’ condition love below, which is usually simpler
Insert
Insert a new record
Update
Update one or many records by querying the records, setting the values on the records, and then calling .update();
Delete
Delete one or many records by querying for the records and then calling the .deleteRecord() method
deleteMultiple (shortcut)
If you are deleting multiple records then the ‘deleteMultiple’ method can be used as a shortcut
addEncodedQuery
Encoded query strings can be copied directly from a filter, by right
This article presents an alternative explanation to the visible behavior of GlideRecord.next() when looping through a Record Set and setting values in an Object or Array. By now we’ve all likely seen that the Object or Array will only hold the value of the last object in the loop. Values of the last record retrieved from GlideRecord.next() are duplicated for the total count of records returned by GlideRecord.query() in the Object or Array.
The GlideRecord below demonstrates the behavior. The query returned 56 records, looping through each, then extracting the record number during each iteration shows the final array only displays the very last item in the record set. That result is the behavior which I’ll look to explain in this article.
Before presenting my thoughts, lets look into the prevailing interpretation of the what’s happening. getValue(), or some variation of type conversion to extracts the precise object value. It is further explained that because GlideRecord.fieldName such as incident.number returns an Object, the only possible value will be that of the very last record, as each push(incident.number) call isn’t really extracting a value but poin
Hello,
(There are existing paginated GR solutions out there. The reason I made this one is to add the seemingly missing support for the bulk of the most commonly used GR methods and page URLs. Knowing me, I overlooked a solution that already offered all of this, but the adventure and learning experience hopefully will make my effort still worth it. Please advise if there is a better solution out there! I'll link to it here, if so. Thanks.
I've added support for most of the GlideRecord methods: addQuery (and its addOrCondition callback), addEncodedQuery, addActiveQuery, addNotNullQuery, addNullQuery, addJoinQuery (all 3 variants and their addConditions callback), orderBy, orderByDesc, and setLimit. I may add more in the future, but those should be the heavy-hitters. Right?)
INTRODUCTION
Have you ever had a need to page through a GlideRecord? For example, say A) your query has nearly 70,000 rows, B) you want to break it up into 30,000 row queries, and C) you want to do this programmatically? The traditional manual way has a lot of little steps I won't list out here for the sake of space. But, back to the point, how do we that? By breaking the query up in