Ryan_Gillespie
Kilo Guru
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();

 

Version history
Last update:
3 hours ago
Updated by:
Contributors