Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Ryan_Gillespie
Kilo Guru

One of the things I've found over the years pretty frequently is the question from junior devs asking about how they can make their 'code' more performant. 

 

There's stuff everywhere and I haven't seen one place that has these little tips and tricks so here we go!

 

1. Use get().

Seriously use get() where you can. If you have the sys_id for an object and are just trying to return one record this is super easy peasy. You don't even HAVE TO HAVE A SYS_ID. You can use a unique value!

 

Here are some examples for how to use it!

 

I can return a specific record:

var gr = newGlideRecord('incident');
gr.get('ENTER-SYS-ID-HERE');

 

I can try to return a specific record and if it doesn't find it, do something:

var gr = newGlideRecord('incident');
if(gr.get('ENTER-SYS-ID-HERE')){
//do something because it found the record
} else {
//do something else because you didn't find the record
}

 

I can use a specific field that holds a unique value:

var incNumber = 'INC0012345';
var gr = newGlideRecord('incident');
gr.get('number', incNumber);

 

2. Use setLimit()

If you are scripting out a query and need to return 1, single record: use setLimit( 1 ). If you are trying to return 10 records, use setLimit(10). This means that you are not going to query the entire table for that result and that means that it'll return faster!

 

Here are some examples for how to use it!

 

If I want to return 1 record:

var incNumber = 'INC0012345';
var gr = newGlideRecord('incident');
gr.addQuery('number', incNumber);
gr.setLimit( 1 ); //spaces added for web formatting - remove the spaces between the 1 and the ( )
gr.query();

 

If I want to return any 10 records for a query:

var ci = 'OfficePC2';
var gr = newGlideRecord('incident');
gr.addQuery('cmdb_ci', ci);
gr.setLimit(10);
gr.query();

 

3. Short-circuit

Using the boolean || will allow you to 'short-circuit' your logic. This means that if you evaluate whether A or B = 1, if it evaluates A = 1, it'll skip B allowing for faster processing.

 

For example, if you are doing this:

if (name.startsWith('a')){
   return true;
} else if (name.startsWith('b')) {
   return false;
} else if (name.startsWith('c')) {
   return true;
}

 

You can instead do this:

if(name.startsWith('a') || name.startsWith('c') ){
  return true;
} else {
  return false;
}

 

Now, if you're talking about queries, here's what ServiceNow has to say:

Common reasons for slow queries

  • A query has too many OR conditions (for more information, see Contains queries and domain access). In the domain hierarchy, place the user or a domain at a hierarchy level where contains or visibility is not needed.
  • The query method is not the domain path query method (for more information, see Domain paths query method😞 If you are not using the domain path query method, contact Customer Service and Support.
  • A query needs a database to be indexed so you can see what is in the database quickly. If you can identify the slow query, run the "explain plan" to see if there are options for indexing available. The "explain plan" is a function of SQL that shows the query and what is going on with it.
 

skeletor.jpg


 

Version history
Last update:
‎10-15-2025 07:17 AM
Updated by:
Contributors