Skip to content

Instantly share code, notes, and snippets.

@mtcoffee
Last active April 1, 2025 19:56
Show Gist options
  • Select an option

  • Save mtcoffee/7e1231e83f29e506e1d8086427d9e6b6 to your computer and use it in GitHub Desktop.

Select an option

Save mtcoffee/7e1231e83f29e506e1d8086427d9e6b6 to your computer and use it in GitHub Desktop.
ServiceNow addExtraField() and CMDB
/*
A sample background script to demonstrate the effectiveness of the addExtraField function.
Already explained well by Rob Fedoruk in https://www.servicenow.com/community/developer-blog/quot-addextrafield-quot-amazing-performance-improvement-to/ba-p/3127640
but we'll put an ITOM spin on it and query some CMDB tables.
*/
var recCI, stringOfCIData, timerStart, timerEnd;
timerStart = new Date().getTime(); // Record start time
recCI = new GlideRecord('cmdb_ci');
recCI.query();
while (recCI.next()) {
stringOfCIData = ('CI Name: ' + recCI.name + ', Model Manufacturer: ' + recCI.model_id.manufacturer.name + ' Asset DisplayName: ' + recCI.asset.display_name);
}
timerEnd = new Date().getTime(); // Record end time
queryRunTime = timerEnd - timerStart; // Calculate execution time in milliseconds
gs.info('CMDB Query execution time before addExtraField: ' + queryRunTime + ' ms');
/*
Now let's run it with the new addExtraField feature
*/
timerStart = new Date().getTime(); // Record start time
recCI = new GlideRecord('cmdb_ci');
recCI.addExtraField('model_id.manufacturer.name'); // gets the value during the retrieval
recCI.addExtraField('asset.display_name'); // gets the value during the retrieval
recCI.query();
while (recCI.next()) {
stringOfCIData = ('CI Name: ' + recCI.name + ', Model Manufacturer: ' + recCI.model_id.manufacturer.name + ' Asset DisplayName: ' + recCI.asset.display_name);
}
timerEnd = new Date().getTime(); // Record end time
queryRunTime = timerEnd - timerStart; // Calculate execution time in milliseconds
gs.info('CMDB Query execution time after addExtraField: ' + queryRunTime + ' ms');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment