Tushar
Kilo Sage
Kilo Sage

I want to shine a spotlight on addExtraField(), a lesser-known GlideRecord method in ServiceNow that’s a total game-changer for anyone dot-walking fields. If you’re writing scripts that dig into reference fields—like caller_id.company.name on the incident table—this one’s for you.

 

So, what’s the deal? When you dot-walk (e.g., incGR.caller_id.company.parent.name), ServiceNow often has to make extra database calls to fetch that nested data. Those round-trips add up, especially in loops or with big datasets. addExtraField() lets you preload those fields in the initial query, cutting out the extra chatter with the database. The result? Faster, cleaner scripts that scale better.

 

I ran a test to see how much it helps, and the numbers are pretty convincing. I queried the incident table 100 times, fetching up to 50 active records each time, and compared dot-walking caller_id.company.parent.name with and without addExtraField.

 

Here’s what I got:

  • Without addExtraField: ~29.8 ms
  • With addExtraField: ~11.8 ms
  • Difference: ~18 ms faster (that’s ~60% quicker with addExtraField!)

This was averaged over five runs to smooth out any noise, and it shows how much those extra lookups hurt without preloading. Your mileage might vary depending on your instance—table size, indexing, caching, and whether fields like caller_id or company are populated all play a role. But in my test, preloading a three-level dot-walk (caller_id.company.parent.name) made a huge difference.

Here’s the script I used to test this, try it yourself:

 

// we will run tests multiple times for averaging
var numIterations = 5;
var resultsWithout = [];
var resultsWith = [];

for (var iter = 0; iter < numIterations; iter++) {
    resultsWithout.push(runTest(false));
    resultsWith.push(runTest(true));
}

var avgWithout = resultsWithout.reduce((a, b) => a + b, 0) / numIterations;
var avgWith = resultsWith.reduce((a, b) => a + b, 0) / numIterations;

gs.info('Average time without addExtraField: ' + avgWithout + ' ms');
gs.info('Average time with addExtraField: ' + avgWith + ' ms');
gs.info('Difference: ' + (avgWith - avgWithout) + ' ms (negative = faster with)');

function runTest(useExtraField) {
    var incGR, startTime = new Date().getTime();
    var totalRecords = 0;
    
    incGR = new GlideRecord('incident');
    incGR.addQuery('active', true);
    if (useExtraField) {
        incGR.addExtraField('caller_id.company.parent.name');  // 3-level dot-walk for better demo
    }
    incGR.setLimit(50); 
    incGR.query();
    
    while (incGR.next()) {
        totalRecords++;
        // Force dot-walk access to trigger lazy-loading if not preloaded
        var dummy = incGR.caller_id.company.parent.name || 'N/A';  //extra queries without addExtraField
    }
    
    var endTime = new Date().getTime();
    return endTime - startTime;
}

Tushar_0-1757433019140.png

 

Thanks,

Tushar

 

Comments
Nikhil Bajaj9
Tera Sage

Hi @Tushar  ,

 

Small but great point. I will also try to see the time difference and performance difference.

 

Regards,

Nikhil Bajaj

Version history
Last update:
yesterday
Updated by:
Contributors