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

Abhinay Erra
Giga Sage

Now try this. few more log statements



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


gs.log(" Cap Resource duration "+current.u_tot_resource_duration_capital.dateNumericValue());


gs.log("Op Resource duration "+current.u_tot_resource_duration_operational.dateNumericValue());



                              current.u_tot_est_project_duration.setDateNumericValue(timems);


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


}


I found that the BR doesn't run just by updating the Days value on an existing record in the related table.   I actually have to add another record to the related table in order for the log entry to display:   Below are the results with the 2 new log statements.



find_real_file.png


I am confused here. What table is the business rule on? And what are conditions on the business rule? can you post a screenshot?


Here's the BR for the Total Operational Resource Duration.   Capital Dur BR is the same.



function onDisplay(current, g_scratchpad) {


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


}


current.u_tot_resource_duration_operational.setDateNumericValue(total);


}


I am totally confused here. One thing at a time. What table are these fields on?



find_real_file.png