Using if condition with setLimit enhance my code??

Community Alums
Not applicable

Hi Everyone,

Suppose, I am having a code:-

var count=0;
var gr= new GlideRecord('incident');
gr.addEncodedQuery('active=true');
gr.query();

if(gr.next()){
    count++;
}
gs.info(count);

Here I am getting count as 1. {Because I am using if condition.}

------------Now Suppose I am enhancing my code as below-------------
var count=0;
var gr= new GlideRecord('incident');
gr.addEncodedQuery('active=true');
gr.setLimit(1);
gr.query();

if(gr.next()){
    count++;
}
gs.info(count);
---------------------------------
In this code I have added the set Limit , although my count is same i.e 1.
Now, I am having one query...does applying setLimit , it is enhancing my process time or both the code will take same time to process...??
And where we can calculate the process time..??

Thanks in Advance
Utsav
2 REPLIES 2

Vishal Jaswal
Giga Sage

Hellow @Utsav JAISWAL 

If you run these two in Background scripts, then the output shows you Script execution history and recovery available here (this will have hyperlink): Table: sys_script_execution_history

vishal_jaswal_0-1741716184571.png

Without setLimit()

vishal_jaswal_1-1741716292109.png
With setLimit() 

vishal_jaswal_2-1741716309883.png


you can clearly see the difference why setLimit(1) is always recommended as best practice where you want to work with only one record in the table.

Hope it helps!

 



 

 

 

 


Hope that helps!

Shivalika
Mega Sage

Hello @UTSAV JAISWAL 

 

Without a doubt using setLimit enhances the performance. This is very evident because in servicenow it's recommended to process 1 million records in a batch of 10000 and is called very efficient. But running 1 million records is not. So, if you want to work on a limit of records, using setLimit is definitely recommended. 

 

Also as @Vishal Jaswal already mentioned, you can run this in background script and check the response time system took. Also syslog_transaction table will store exact time. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway,

 

Regards,

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY