GlideRecord query record returns all records? Require SetLimit?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2023 01:13 PM
Sort of a strange question. When doing a GlideRecord query(), are all records found prior to getting to the IF or WHILE statement? The reason I ask, If you expect only a single record to be returned, should you still use setLimit(1) even though you know only 1 record will be returned?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2023 01:22 PM
Hi @jamesjurden
If you know its only one record there then no need to put setLimit().
For example,
You have incident with sys_id as 12345 then when you GlideRecord on incident table then you can add query like belo
var gr = new GlideRecord('incident');
gr.addQuery('sys_id','12345);
gr.query();
if(gr.next()) {
//Do needful
}
Here we know only one record will be there so no need to use setLimit.
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2023 07:27 PM
Hi James,
As you know it now only 1 record will be returned, but things can change in future. There can be some bad data inserted or duplicated data inserted which satisfies your query. In that case, setLimit will be helping you out. Although, ServiceNow doesn't guarantee which record will be returned if multiple records satisfy the condition. However, you will come to know when an incident is created in that case.
I would advise you to have the setLimit line of code. Also, while you're traversing the GlideRecord, use if(gr.next()) instead of while loop. Hope it makes sense.
Please mark this comment as Correct Answer/Helpful if it helped you.
Cheers,
Hardit Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2023 09:06 PM
Hi @jamesjurden ,
you can understand query() function in this way: it makes the GlideRecord understand the "scope" (based on the query condition) of the records it's about to process.
So, if your query condition only returns one record (e.g., sys_id equals to a value), using setLimit(1) or not will have the same performance.
However, if the query condition matches thousands of records, but eventually you will only take 1 record, setLimit(1) will make a great difference in performance.
Please see and try the following code snippet (my incident table has ~7000 records).
The reason is that if we have setLimit set, query() function will only need to take a glance at a much smaller "scope" instead of the whole "scope" matched by the query condition.
var gr = new GlideRecord("incident");
var start = Date.now();
// gr.setLimit(1);
gr.query();
if(gr.next()){
gs.info("***"+gr.getValue("short_description"));
}
var end = Date.now();
gs.info("###"+(end-start));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2023 09:26 PM
Hi @jamesjurden ,
It depends upon whether your are using any filter or not while doing the Glide Record & also which kind of statement you use if or while.
- GlideRecord() without query(addQuery() or addEncodedQuery()) & using 'IF' loop will return you only a single record from the table which you are querying.
GlideRecord() without query(addQuery() or addEncodedQuery()) & using 'while' loop will return you all the records from the table which you are querying.- GlideRecord() with query(addQuery() or addEncodedQuery()) & using 'if' loop will return you only single record from the table which you are querying dependent upon the query being matched. If the query doesn't match no records will be returned.
- GlideRecord() with query(addQuery() or addEncodedQuery()) & using 'while' loop will return all the records from the table which you are querying dependent upon the query being matched. Lets say the query matches for 10 records then 10 records will be returned in this scenario, If the query doesn't match no records will be returned.
Hope this helps. Please mark my answer helpful & accepted if it resolves your query.
Thanks,
Danish
