Need some help with a GlideAggregate sum script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 06:30 AM
Hi SNC,
Case goes like this - I have a field on the problem form (u_total), that I want to be populated with the sum of the values from the field 'u_impacted_allocation' in each related to the problem incident. Below is my script which does not sum them up currently. What am I missing?
(function executeRule(current, previous /*null when async*/) {
var gr1 = new GlideAggregate('incident');
gr1.addQuery('problem_id', current.sys_id); //finds the outages for the incidents related to the current problem
gr1.addAggregate('SUM','u_impacted_allocation');
gr1.query();
while(gr1.next()){
var total = gr1.getAggregate('SUM','u_impacted_allocation'); //calculates the total proportional impact allocation
current.u_total = total; //sets the calculation to the field on the problem form
current.update();
}
})(current, previous);
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2017 01:02 AM
Ok, so after all of your feedback plus some experimenting on my side, I think I found the solution. Below is the code with comments:
(thanks to everyone for the ideas btw!)
(function executeRule(current, previous /*null when async*/) {
var gr1 = new GlideAggregate('incident');
gr1.groupBy('problem_id'); //groups the incident by related problem
gr1.addQuery('problem_id', current.sys_id); //finds the incidents related only to the current problem
gr1.addAggregate('SUM','u_impacted_allocation'); //sums the impacted allocation values from the related incidents
gr1.query();
while(gr1.next()){
var total = gr1.getAggregate('SUM','u_impacted_allocation'); //populates the impacted allocation in the problem
current.u_impacted_allocation = total; /setting the field in the problem record to the variable carrying the sum/
current.update();
}
})(current, previous);
This works like a charm, so consider this closed:)