
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2016 11:37 AM
I have parent records with child records. The child records each record unique values of impacted users. When I attempt to use glideAggregate to sum the individual child values into a single field in the parent record, I get no value returned.
Any help is appreciated:
var rec = new GlideAggregate('u_so_incident_impacts'); |
rec.groupBy('number'); | |
rec.addAggregate('SUM','u_users_in_game_impacted'); |
rec.query(); | |
while(rec.next()){ | |
var count = rec.getAggregate('SUM','u_users_in_game_impacted'); |
var usam = count.addAggregate('SUM','u_total_users_impacted'); | |
current.u_total_users_impacted = usam; | |
// find and store the count value in the related record. | |
} |
I found the original script on another thread found here:
Add values from field on a related list
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 10:26 AM
OK, here's the script I used in the calculated field on the dictionary entry for incident.u_total_users_impacted...
(function calculatedFieldValue(current) {
var childTable = 'u_so_incident_impacts';
var numberField = 'u_users_in_game_impacted'; // what do we want to sum up?
var count = 0;
var gr = new GlideRecord(childTable);
gr.addQuery('u_parent', current.sys_id);
gr.query();
while (gr.next()) {
var n = parseInt(gr.getValue(numberField), 10);
count += n;
}
return count; // return the calculated value
})(current);
Something to keep in mind... the calculated field isn't STORED in the database until you update the record. So you MAY incur performance issues if you present a list and it has to do all these gliderecord lookups to show you the list (assuming you put that field in the list layout.)
To improve performance, do a one-time forced-update to the incident records so it retally's the numbers and stores them. Going forward, they'll maintain themselves.
var inc = new GlideRecord('incident');
inc.query();
while (inc.next()) {
inc.setWorkflow(false);
inc.autoSysFields(false);
inc.setForceUpdate(true);
inc.update();
}
You can run this from scripts background or create a Fix script if you like (makes it easier to move from dev to prod in an update set.)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 06:24 AM
Ok. I've commented out line 10 and restored the later lines. I'm still getting this error when it attempts to calculate.
"Unique Key violation detected by database (Duplicate entry 'org.mozilla.javascript.Undefined' for key 'PRIMARY')"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 06:28 AM
That's bizarre. Let me see if I can reproduce it (now that I know your tables and fields better.) Stand by.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 08:57 AM
Hi Chuck - I hope you had a good weekend. Did you have the opportunity to see if you could reproduce the error?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 09:02 AM
I have not.
Next question - Are you triggering this from a business rule? (I see current.sys_id as the parentField name)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 09:03 AM
No. Currently this is a calculated field value.