Background script returns 0 count for table

ServiceNowVS
Tera Expert

Hi Team,

 

I am facing a very weird issue. I am trying to get count of records in a table using background script. However i am getting count as 0. even though there are data in that table. This is happening in DEV instance., Working fine in other instances. Here is my script:

 

var getCount = new GlideRecord('<<tablename>>');
getCount.addQuery('sys_id', <<sys_id>>);
getCount.query();
gs.print("Count is  "+ getCount.getRowCount());
 
Even if i dont put query condition in line 2, in every case count is coming as 0.
 
There are ACL's restricting data but that should not affect getting counts from backend right? Tried fix script too.
 
Anybody else faced this?
9 REPLIES 9

Shubham Singh
Mega Guru

Hi @ServiceNowVS 

 

Check if you are passing the table name and sysid properly. Your sysid should be 32 chars UID (count it).

 

Alternate approach:

var gr = new GlideRecord('<<tablename>>')
if(gr.get(‘<<sys_id>>’){
     gs.info(gr.sys_id);
}
 
Thanks!
 
Please mark this response as correct and helpful ✔️👍

Hi Shubham,

Yes i have tried the same script in QA instance and it is giving me count. Even though i dont query certain sys_id, it is still giving count as 0

Danish Bhairag2
Tera Sage
Tera Sage

Hi @ServiceNowVS ,

 

If you're facing an issue where you're unable to retrieve records count using a GlideRecord query despite having data in the table, it could be due to various reasons. Here are a few things to check and consider:

 

1. **Permissions & ACLs:**

   - Ensure that the user executing the script has sufficient permissions to access the table and read the records.

   - Check the ACLs (Access Control Lists) on the table. Even though ACLs shouldn't directly affect retrieving counts, it's worth reviewing them to ensure they're not causing unexpected behavior.

 

2. **Instance-specific Issues:**

   - Check for any instance-specific configurations or customizations that might impact data retrieval.

   - Compare the configurations between the working instances and the problematic one. Differences might give clues about the issue.

 

3. **Cache or Indexing:**

   - Check if there are any issues related to indexing or cache. Sometimes, issues in indexing can lead to unexpected results in record retrieval.

   - Try clearing the cache or re-indexing the table to ensure data is accurately reflected.

 

4. **Error Handling:**

   - Check for any errors or exceptions in the script execution by adding error handling.

   

Here's an updated version of your script with error handling to identify any potential issues:

 

var getCount = new GlideRecord('<<tablename>>');

getCount.addQuery('sys_id', '<<sys_id>>'); // Replace <<tablename>> and <<sys_id>> with actual values

getCount.query();

 

if (getCount.error()) {

    gs.print("Error retrieving records: " + getCount.getErrorMessage());

} else {

    gs.print("Count is " + getCount.getRowCount());

}

 

This script includes error handling to print an error message if there's an issue with the query execution.

 

If the issue persists, consider reaching out to your ServiceNow instance administrator or support team to investigate further, as this could be related to instance-specific configurations or issues.

 

Thanks,

Danish

 

Hi Danish,

All the configurations are same for DEV & QA instance.

I have tried to run the updated version and it is going to else part and printing count as 0