Is there any way to count the total number of records in a table?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 01:22 PM
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++;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 01:25 PM
Hi Brittany,
Use:
var gr = new GlideRecord("table_name");
gr.query();
var count = gr.getRowCount();
gs.log(count);
in background script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 01:42 PM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-03-2018 12:57 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 01:25 PM
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.