AddExtraField() method in ServiceNow

Sumanth16
Kilo Patron

The addExtraField() method in ServiceNow is typically used in the context of creating or customizing scripts, like client scripts or business rules, when working with GlideRecord. This method is used to add a field that is not part of the default GlideRecord query.

 

Here's a basic example of how you can use the addExtraField() method with GlideRecord in ServiceNow:

 

var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.addExtraField('caller_id'); // Adds the caller_id field to the query result
gr.query();

while (gr.next()) {
gs.info('Incident Number: ' + gr.number + ', Caller ID: ' + gr.caller_id);
}

 

Explanation:

  • addExtraField('caller_id'): This line ensures that the caller_id field is included in the returned record, even if it's not directly involved in the query.
  • It can be particularly useful when you're working with large data sets where you need additional fields that aren't being used as filters in your query.

This method can be used to optimize performance by including fields in your query results without directly filtering by them.

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

I was not aware of this new (Washington DC?) method, but didn't understand its purpose with this example as caller_id is on the incident table, so it is already included in the default GlideRecord query.  Reading the Docs site explanation, a better basic example to clearly illustrate this method would be

var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.addExtraField('caller_id.company.name'); // Adds the Name of the Company of the Caller field to the query result
gr.query();
while (gr.next()) {
    gs.info('Incident Number: ' + gr.number + ', Caller's Company: ' + gr.caller_id.company.name);
}

Note that the same script will yield the same results without adding the .addExtraField method, but this method optimizes the query with only one trip to the database, so could have a significant impact on GlideRecord performance in larger queries/scripts.

Daniel Oderbolz
Kilo Sage

I totally agree with @Brad Bowman .

The purpose of this function is to include fields you want to dot-walk.

This offers a performance benefit as otherwise, additional queries would be required.

However IMHO, ServiceNow should be able to derive this information statically from the code without the developer having to provide this information.


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel