Need some help with a GlideAggregate sum script

mitzaka
Mega Guru

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);

10 REPLIES 10

Rovan1
Tera Contributor

I had similar issue and the group by fixed it. thanks mate.

Cory CJ Wesley
Kilo Sage
Kilo Sage

Off the top of my head - I don't think you can set values to fields inside of a GlideAggregate lookup.   So one of the things I'd do is turn this:



  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();


  }



into this:



  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(); //Since this is a business Rule only use this if this is an After or Async Business Rule.





You can set inside the GlideAggregate while/if loop


Good to know.


Abhinay Erra
Giga Sage

What is the field type of impacted allocation and total?