Get Total of Two Duration Fields

michaelcory
Giga Expert

I have 3 fields on a Demand form (see screen shot below). Need to get the total of the Capital and Operational Resource Duration fields to display in the "Total Estimated Project Duration" field. I am using the BR shown below (Total Estimated Project Duration) but it is not working.   System Log shows that the BR is running.

From the screen shot below, the Total Capital Resource Duration and Total Operational Resource Duration get their values from 2 Business Rules that run on the Demand Table (See Related Lists below).   Below the Related LIsts is the BR that runs on the Demand table to get the Operational Resource Duration …The Capital BR is configured exactly the same.

find_real_file.png

BR =   Total Estimated Project Duration - Runs on Demand Table

function onBefore(current, previous) {

    //This function gets total duration by adding the Capital and Operational Resource durations

                              var timems = 0;

                              var dur = 0;

                              dur = current.u_tot_resource_duration_capital.getNumericValue();

                              timems += dur;

                              dur = current.u_tot_resource_duration_operational.getNumericValue();

                              timems += dur;

                              current.u_tot_est_project_duration.setDateNumericValue(timems);

                              //gs.log("Did it work");

}

Related Lists on Demand table

Related List — Capital Resources (u_dmn_resources_capital)

    1. Duration field (u_duration)
    2. Sum of Duration records added to Total Capital Resource Duration field (on parent Demand form)

Related List - Operational Resources (u_dmn_resources_operational)

    1. Duration field (u_duration)
    2. Sum of Duration records added to Total Operational Resource Duration field (on parent Demand form)

BR = Total Operational Resource Duration - Runs on Demand table - Gets the sum of all Duration fields in Operational Resources Related List

function onDisplay(current, g_scratchpad) {

    //This function queries the Related List records and adds the sum of durations to the Total Duration field on parent Demand

                              var total=0;

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

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

                              grid.query();

                              while(grid.next()){

                                                              total+= grid.u_duration.dateNumericValue();

}

  1. current.u_tot_resource_duration_operational.setDateNumericValue(total);// Total Duration field on Demand form

}

1 ACCEPTED SOLUTION

Inactivate other two display business rules on the demand table. You only need one display business rule and the following script should go in there. I am putting everything in one business rule.




function onDisplay(current, g_scratchpad) {


  var total=0;


  var total1=0;


  var grid = new GlideRecord('u_dmn_resources_operational'); //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_operational.setDateNumericValue(total);


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


  grid1.addQuery('u_demand',current.getValue('sys_id'));//reference field on related list table


  grid1.query();


  while(grid1.next()){


  total1+= grid1.u_duration.dateNumericValue();//duration field on related list table


  }


  current.u_tot_resource_duration_capital.setDateNumericValue(total1);//Total Cap Dur field on Demand Form


  current.u_tot_est_project_duration.setDateNumericValue(total+total1);


}


View solution in original post

25 REPLIES 25

Yes, it is Fuji.


Your onAfter function syntax is wrong. Use this



function onAfter(current, previous) {


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


  var query = 0;



  var grid = new GlideRecord('u_dmn_resources_operational'); // table name


  grid.addQuery('u_demand', ps);


  grid.query();


  while(grid.next()){


          query += parseFloat(grid.u_total_cost);


  }



  var gr = new GlideRecord('dmn_demand');


  gr.addQuery('sys_id', ps);


  gr.query();



  while(gr.next()){


  gr.setValue('u_tot_operational_resource_cost', query);


  gr.update();


  }


}


Thanks Abhinay.   I corrected the function in the Cost BR and 3 other BR's with the same function.   I'm still not getting a value in the Total Estimated Project Duration field after applying the changes to the BR recommended above.   Sys Log does show that it is running, just not setting anything in the field.


find_real_file.png


BR = Total Estimated Project Duration


function onBefore(current, previous) {


    //This function gets total duration by adding the Capital and Operational Resource durations


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


                              //gs.log("Did it work");


Abhinay Erra
Giga Sage

Can you put the log statement to see the value of timems as shown below. Let me know what the log shows



function onBefore(current, previous) {


    //This function gets total duration by adding the Capital and Operational Resource durations


                              var timems = 0;


                              var dur = 0;


                              gs.log("debug 1 "+timems);


                              dur = current.u_tot_resource_duration_capital.dateNumericValue();


                              timems += dur;


                              gs.log("debug 2 "+timems);


                              dur = current.u_tot_resource_duration_operational.dateNumericValue();


                              timems += dur;


                                  gs.log("debug 3 "+timems);


                              current.u_tot_est_project_duration.setDateNumericValue(timems);


                              //gs.log("Did it work");


}


Getting 0 value for all 3 debugs



find_real_file.png