Is there any way to count the total number of records in a table?

brittanyeccles
Giga Contributor

Is there any way to count the total number of records in a specific table? It doesn't have to have a certain field, I just want to count the total number of records.

I have already tried to use the following, but neither of them worked:

var count = new GlideAggregate('tableName');

count.addAggreate('COUNT');

count.query();

var records = 0;

if (count.next())

{

        records = count.getAggregate('COUNT');

}

and also:

var grCount = new GlideRecord('tableName');

grCount.query();

var count = 0;

while (grCount.next())

{

        count++;

}

6 REPLIES 6

Jaspal Singh
Mega Patron
Mega Patron

Hi Brittany,



Use:



var gr = new GlideRecord("table_name");


gr.query();


var count = gr.getRowCount();


gs.log(count);



in background script.


Thank you for taking the time to reply back! However, I am getting an error message in the console that states that the "query must be called with a callback function." Do you have an idea as to what it means by this?


This approach will work, but it is not best for performance.  This will fetch every record from the database before counting them, which could impact performance for larger tables.  Use GlideAggregate and COUNT when you just need to determine number of rows in a query.

SanjivMeher
Kilo Patron
Kilo Patron

It should be in below format



var count = new GlideAggregate('your table name');


count.addAggregate('COUNT');


count.query();


var recCount = 0;


if (count.next())


    recCount = count.getAggregate('COUNT');



gs.log('My record count is '+recCount);



Please mark this response as correct or helpful if it assisted you with your question.