Tejas Adhalrao
Mega Sage

Introduction

Have you ever tried exporting a large number of records from a ServiceNow table to Excel?

When working with large tables containing thousands or even lakhs of records, exporting everything in a single operation can become challenging. Depending on the export method and instance configuration, users may experience export limitations, performance issues, or incomplete downloads.

I recently worked on a solution to make large record exports more manageable.

Instead of exporting all records at once, I implemented a Batch-Based Excel Export Solution using ServiceNow Data Broker Transform scripts and a simple user interface.

The idea is straightforward: divide the total records into smaller batches of 10,000 and allow users to export the required batch independently.

Let's understand how it works.

 

  1. Solution Overview

The solution provides a clean, user-friendly interface where users can:

  • View the selected ServiceNow table.
  • Check the total number of records available in that table.
  • See the configured batch size (10,000 records).
  • Select a specific batch range from a dropdown menu.
  • Click the Export button to download the selected batch directly in Excel format.

The system automatically calculates the total record count of the chosen table and dynamically creates batches of 10,000 records.

For example, consider an Incident table containing 66,678 records. The solution automatically detects this count and generates seven batches

Batch

Record Range

Records

Batch 1

1–10,000

10,000

Batch 2

10,001–20,000

10,000

Batch 3

20,001–30,000

10,000

Batch 4

30,001–40,000

10,000

Batch 5

40,001–50,000

10,000

Batch 6

50,001–60,000

10,000

Batch 7

60,001–66,678

6,678

 

The number of batches is completely dynamic and adjusts based on the table's record count:

  • If a table has 5,000 records, only 1 batch is created.
  • If a table has 25,000 records, 3 batches are created.
  • If a table has 66,678 records, 7 batches are created, with the final batch automatically adjusting to include the remaining 6,678 records.

 

  1. User Interface (Workspace Experience)

I created a simple Workspace Experience page in UI Builder so end users can export data without needing technical knowledge.

At the top of the page, users see three key details:

  • Table: The selected ServiceNow table (e.g., Incident).
  • Total Records: The total count of records in that table.
  • Batch Size: The maximum number of records per download (10,000).

 

TejasAdhalrao_0-1796111443273.png

 

users can open the Select Batch dropdown to view all available record ranges and click Export to trigger the Excel download.

TejasAdhalrao_1-1796111471131.png

TejasAdhalrao_2-1796111494540.png

 

  1. Technical Implementation

Behind the scenes, the solution uses two ServiceNow Data Broker Transform scripts. Separating the logic keeps the solution clean and easy to maintain.

Data Broker 1: Get_Records_Batch

Purpose: Queries the selected table using GlideAggregate to get the total record count, calculates how many 10,000-record batches are needed, and returns the dropdown options (label and record range values) to UI Builder.

 

function transform(input) {

    var table = input.table;
    var batchSize = 10000;

    var result = [];

    if (gs.nil(table)) {
        return result;
    }

    // Validate that the table exists
    var tableGR = new GlideRecord(table);

    if (!tableGR.isValid()) {
        return result;
    }

    // Get total record count
    var ga = new GlideAggregate(table);
    ga.addAggregate('COUNT');
    ga.query();

    var totalRecords = 0;

    if (ga.next()) {
        totalRecords = parseInt(
            ga.getAggregate('COUNT'), 10
        );
    }

    // Generate batches of 10,000
    for (var start = 0;
         start < totalRecords;
         start += batchSize) {

        var end = Math.min(
            start + batchSize,
            totalRecords
        );

        result.push({
            id: start + "-" + end,
            label: (start + 1) + "-" + end
        });
    }

    return result;
}

 

 

Data Broker 2: Get_URL_to_export_records

Purpose: Takes the selected table name and batch range as inputs, constructs the proper ServiceNow Excel export URL (using sysparm_first_row and sysparm_view / query parameters), and returns the download link to the Export button.

function transform(input) {

    var table = input.table;
    var baseQuery = 'numberISNOTEMPTY';

    var range = input.range;

    // Parse selected range
    var parts = range.split('-');

    var start = parseInt(parts[0], 10);
    var end = parseInt(parts[1], 10);

    var countToExport = end - start;

    // Build export query
    var exportQuery =
        baseQuery + '^ORDERBYsys_created_on';

    // Generate Excel export URL
    var exportUrl = '/' + table +
        '_list.do?EXCEL&' +
        'sysparm_query=' +
        encodeURIComponent(exportQuery) +
        '&sysparm_offset=' + start +
        '&sysparm_limit=' + countToExport +
        '&sysparm_fields=number,short_description,sys_id';

    gs.info('Working Export URL: ' + exportUrl);

    return exportUrl;
}

 

  1. Conclusion & Update Set

 

Handling large-volume data exports in ServiceNow can be challenging, especially when users need to export thousands of records.

With this batch-based export approach, we can make the process more manageable by automatically calculating the available records and dynamically generating batches of 10,000 records.

The solution provides a simple UI Builder interface where users can select the required batch and export the records independently.

The best part is that the number of batches is not hardcoded. It is automatically determined based on the actual number of records available in the selected table.

I have also attached the Update Set for anyone interested in exploring this implementation.

You can import the Update Set into your Personal Developer Instance (PDI), preview it, and commit it to explore the UI Builder page and Data Broker Transform scripts.

I hope this implementation helps other ServiceNow developers who are looking for ways to handle large-volume record exports.

Have you implemented a similar solution, or do you use another approach for exporting large datasets? Share your thoughts and suggestions in the comments!

 

Regards,
Tejas