How to fix it

  1. First, verify the user context by adding gs.log('User: ' + gs.getUserName() + ', Admin: ' + gs.hasRole('admin')) before both your GlideAggregate and comparison queries.
  2. Check System Logs > System Log > All to see if the user context differs between queries. If yes, force the same context using gs.getSession().impersonate('username') before your aggregate.
  3. Identify null value issues by running a GlideRecord query with addNullQuery('your_groupby_field') to count records with empty groupBy fields.
  4. If null values are the issue, either remove the groupBy clause or add addNotNullQuery('your_groupby_field') to both queries for consistency.
  5. For domain issues, check current domain with gs.getSession().getCurrentDomainID() and set to global domain using gs.getSession().setDomainID('global') before the aggregate if needed.
  6. Debug query differences by logging both encoded queries: add gs.log('Aggregate query: ' + ga.getEncodedQuery()) and gs.log('Record query: ' + gr.getEncodedQuery()).
  7. Copy the encoded query from your aggregate and paste it into the list view filter to verify the same records are returned visually.
  8. For table inheritance issues, check the sys_class_name field and add addQuery('sys_class_name', 'table_name') to both queries if you need to restrict to specific table types.
💡

Always test your fix by running both queries with identical encoded queries and user context, then verify the counts match before deploying the business rule.