Efficient way to return record counts against selected tables...

SB87
Tera Expert

I'm extracting data out of ServiceNow for around 100+ tables. I then need to roughly compare the counts to prove the extraction hasn't missed any records. I do this currently by making a Rest API Explorer request for each of the tables and taking the X-Total-Count.

There must be a more efficient way I can do this. Anyone have any suggestions?

Thanks.

2 REPLIES 2

Sonam_Tiwari
Kilo Sage

How about using a common function for GlideAggregate and getting the count

 

 

 

function getRecordCount(tableName) {
    var gr = new GlideAggregate(tableName); 
    gr.addAggregate('COUNT');
    gr.query();
    if (gr.next()) {
        return gr.getAggregate('COUNT');
    }
    return 0; // Return 0 if no records found
}

Sample - replace table names
var table1Count = getRecordCount('table1');
var table2Count = getRecordCount('table2');
// ... Repeat for other tables

gs.info('Table 1 record count: ' + table1Count);
gs.info('Table 2 record count: ' + table2Count);
// ... Log counts for other tables

 

 

 

Run in the background with required table names and it should easily get you all the counts.

 

sonamtiwari_0-1709124081593.png

 

 

 

Consider indicating the response as helpful and marking it as correct if it meets your needs.

Ankur Bawiskar
Tera Patron
Tera Patron

@SB87 

you can also use Aggregate API

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader