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

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


Thank you Abhinay!.   Works perfectly



I have a 3rd BR on the Demand form that gets the total of Capital and Operational Duration fields (screen shot below).   It was working when I entered values manually in Oper and Capital Duration fields, but now does not get the total of the two when they get entered by other BR.   The BR to get "Total Estimated Project Duration" is posted below.   Any ideas on that one?



function onBefore(current, previous) {


  var timems = 0;


  var dur = 0;


  dur = current.u_tot_resource_duration_capital.dateNumericValue();


  timems += dur;


  dur = current.u_tot_resource_duration_operational.dateNumericValue();


  timems += dur;


  current.u_tot_est_project_duration.setDateNumericValue(timems);


}


find_real_file.png


What are the conditions on this BR, Post a screenshot of the BR


find_real_file.png


find_real_file.png