Get Total of Related List Duration Values & Add to Parent Total Field

michaelcory
Giga Expert

I have a Related List on the Demand form.   The related list table has a Duration field.   There may be 1 or several records created in the Related List with Duration values.   I need to get the Total of all duration values in the records from the Related List and add to a Total field on the Parent (Demand form).   I was provided a great Community solution for how to do this with Total Costs (link), but am struggling with the duration format.   Below is the business rule solution that was provided and works for the Cost field totals.   I tried applying the business rule to the Related List table that has the duration field.   I modified the script by adding getNumericValue to query duration and setDateNumericValue   to the value set on the Demand record,but doesn't work.   Tried various other business rules to no avail.   Any suggestions?

(function onAfter(current, previous /*null when async*/) {

  var pd = current.u_demand; //reference field to parent demand

  var query = 0;

  var grid = new GlideRecord('u_dmn_resources_capital'); //related list table name

  grid.addQuery('u_demand', pd);

  grid.query();

  while(grid.next()){

          query += grid.u_duration.getNumericValue();

  }

  var gr = new GlideRecord('dmn_demand');

  gr.addQuery('sys_id', pd);

  gr.query();

  while(gr.next()){

  gr.setDateNumericValue('u_tot_resource_duration_capital', query);//Duration Total on Parent Demand

  gr.update();

  }

})(current, previous);

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

You need a display business rule on the demand table for this. And put this code in there



var total=0;


var grid = new GlideRecord('u_dmn_resources_capital'); //related list table name


  grid.addQuery('u_demand', current.getValue('sys_id'));


  grid.query();


  while(grid.next()){


total+= grid.u_duration.dateNumericValue();


}


current.u_tot_resource_duration_capital.setDateNumericValue(total);


View solution in original post

12 REPLIES 12

Abhinay Erra
Giga Sage

Everything looks ok. Are you sure you changed the duration values to trigger the BR?


I just created a new Demand Record and then added a duration value to the Capital and Operational Resource Related Lists.   Getting the totals for those, but still not getting the Grand total of both (Total Estimated Project Duration).



find_real_file.png


Abhinay Erra
Giga Sage

Please mark my response as correct.



If you are viewing this from the community inbox you will not see the correct answer button. You need to open the thread directly and you will see a correct answer button with red star.     Please review this How To Mark Answers Correct From Community Inbox


Abhinay Erra
Giga Sage

Can you insert a   log statement in the business rule and see if the business rule is triggered at all?


Added a gs.log(current.u_tot_resource_duration_capital); to BR.   Didn't get a result in System Log


find_real_file.png