GlideAggregate Count Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2016 08:42 AM
Hello,
I need some help with GlideAggregate script. I need to COUNT number of records in the table, which have value that is larger then 100 in the "field1". Currently my script works only after I create new record, then go back to that record and update it. I want the script to work immediately after new record is created. Currently this Business Rule runs "Before" on Insert and Update. I tried switching to "After" but it stops working all together.
Application name:
ERR Test
Table name:
Test
Business Rule Script:
var count = new GlideAggregate('x_depar_err_test_test');
count.addQuery('field1','>',100);
count.addAggregate('COUNT');
count.setGroup(false);
count.query();
var amount = 0;
if (count.next())
amount = count.getAggregate('COUNT');
current.count = amount;
Additional question unrelated to the script issue, why some tables I create start with "x_snc_" and this one starts with "x_depar_".
Thank you very much
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2016 09:17 AM
Hi Adam,
If you're populating a field on the current record you'll need to run it as a before insert/update business rule. That means that this script runs and assigns the value to the count field before the update. If you run it after it will assigned value after the record is written to the database so your value won't end up being saved. It should populate your field without going back and updating your record if you have the insert checkbox checked. You might add some logging to validate when it is running and what the values are.
x_snc_ and x_depar_ are the names of different scopes. Any scoped application you create will have x_depar_ and that is specific to your company. x_snc_ means it was created as a scoped application by ServiceNow.