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

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi Mike,



That is since glideDuration doesn't use getNumericValue();



This should work in your first BR:



var duration = new GlideDuration(current.u_total_capital_resource_duration.getDurationValue());


var duration2 = new GlideDuration(current.u_total_operational_resource_duration.getDurationValue());


current.u_total_estimated_project_duration = duration.add(duration2);




//Göran


Abhinay Erra
Giga Sage

Your method is incorrect here. You should use dateNumericValue() instead of getNumericValue()



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


}


Tried both solutions proposed but still 0 display in Total Estimated Project Duration field.   I see Warnings in Sys Log on another BR that runs on the Related table (Operational Resources).   Wonder if this has something to do with the issue. I added that Business Rule below the screenshot of the Sys log warnings.


find_real_file.png


BR = Total Operational Resource Cost - Runs on Operational Resources related table - Puts Total Cost in Total Operational Resource Cost field on parent Demand

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



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


  }



})(current, previous);


Which release are you on? Fuji?