Enhancing GlideRecord Performance with addExtraField() in ServiceNow - ben-vargas/servicenow-wiki GitHub Wiki

Enhancing GlideRecord Performance with addExtraField() in ServiceNow

ServiceNow's Washington DC release introduces a significant enhancement to the GlideRecord API: the addExtraField() method. This method streamlines the retrieval of dot-walked fields, leading to improved script efficiency and performance.

Understanding Dot-Walked Fields

In ServiceNow, dot-walking allows access to fields on referenced records. For example, accessing the company name of a caller in an incident involves traversing multiple references: incident.caller_id.company.name. Traditionally, retrieving such data required multiple database queries, impacting performance.

Introducing addExtraField()

The addExtraField() method enables developers to include dot-walked fields directly in a GlideRecord query, ensuring all necessary data is fetched in a single database call. This approach reduces the overhead associated with multiple queries and enhances script performance.

Syntax:

GlideRecord.addExtraField(fieldName);
  • fieldName: A string specifying the dot-walked field to include in the query.

Practical Example

Consider a scenario where you need to retrieve the caller's name and their company's name for a set of incidents.

Without addExtraField():

var gr = new GlideRecord('incident');
gr.query();
while (gr.next()) {
    var callerName = gr.caller_id.name;
    var companyName = gr.caller_id.company.name;
    gs.info(callerName + ' works at ' + companyName);
}

In this script, accessing gr.caller_id.name and gr.caller_id.company.name may result in additional database queries for each dot-walked field, especially if these fields are not part of the default selected fields.

With addExtraField():

var gr = new GlideRecord('incident');
gr.addExtraField('caller_id.name');
gr.addExtraField('caller_id.company.name');
gr.query();
while (gr.next()) {
    var callerName = gr.caller_id.name;
    var companyName = gr.caller_id.company.name;
    gs.info(callerName + ' works at ' + companyName);
}

By using addExtraField(), both dot-walked fields are included in the initial query, reducing the need for additional database calls and enhancing performance.

Performance Benefits

Implementing addExtraField() offers several advantages:

  • Reduced Database Calls: Fetches all necessary data in a single query.

  • Improved Script Efficiency: Minimizes the complexity and execution time of scripts.

  • Enhanced System Performance: Lowers the load on the database, leading to better overall system responsiveness.

Considerations

  • Compatibility: Ensure your ServiceNow instance is updated to the Washington DC release or later to utilize this method.

  • Field Validity: Verify that the specified dot-walked fields exist to prevent runtime errors.

  • Use Cases: Ideal for scenarios requiring access to multiple related fields, such as reporting or complex business logic implementations.

Conclusion

The addExtraField() method is a powerful addition to the GlideRecord API, offering a streamlined approach to handling dot-walked fields. By incorporating this method into your scripts, you can achieve significant performance improvements and write more efficient, maintainable code.

For more detailed insights and practical demonstrations, refer to the following resources:

By leveraging addExtraField(), developers can enhance the performance and efficiency of their ServiceNow applications, leading to a more responsive and robust platform experience.