Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Calculate combined duration

Rshear
Kilo Expert

Hi all,

A simple query for many..although it escapes me..(i have trawled the forums and unable to locate a defined answer).

Question:

I have 3 duration type fields (x, y and z) and wish to add these together to reveal a combined duration field (a). Sounds simple...although I cant for the life of me get this working. I

Summary: x + y + z = a

Tried client script (onChange)

var design = g_form.getValue('u_design');

var build = g_form.getValue('u_build');

var test = g_form.getValue('u_test');

var total = design + build + test;

g_form.setValue('u_total', total);

Any help, much appreciated...once done with that I then need to multiply that value by a 'b' to provide a cost..however one step at a time.

7 REPLIES 7

m_ahmedmod
Giga Contributor

Hi Russell,



Is there any reason why this has to be configured using a client script?


If not I would suggest a 'before' business rule, something along the lines of:



calculateDuration();

function calculateDuration(){
   var total = (current.u_duration_1.dateNumericValue() + current.u_duration_2.dateNumericValue() + current.u_duration_3.dateNumericValue());
   current.u_duration_total.setDateNumericValue(total);
}


That would then give you the option to carry out further calculations with the variable 'total'.



Hope that helps.


Hi Mohammed



I wonder if you could help me further on this one? (BTW the above worked thank you )



I now need to calculate a cost from the total duration.



I've tried to do it the same way, but, its not working.   When I print the current.u_duration_total to the logs, it comes up as a date and time field, and not the number of days IE 3.   So I wanted to do a calculation on daily rate (price) eg £100 * the current.u_duration_total.



I tried the below:



calculateCosts();



function calculateCosts(){


  gs.log("day rate - " +current.u_day_rate);     //Shows correct value


  gs.log(" est effort - " +current.u_estimated_effort);       //showing as a date and time value



  var totalCost = (current.u_day_rate * current.u_estimated_effort.getDisplayValue);



  current.u_effort_cost.setValue(totalCost);


}



What do I need to put on the end of current.u_estimated_effort to get the actual days/hours/minutes?



Thank you in advance


Hi Pennie,



The calculation for estimated effort is actually returned in milliseconds, so you will have to do the following calculation to get days:



current.u_estimated_effort/(1000*60*60*24)


The function is likely to look like this:



function calculateCost(effort){
          var dayEffort = (effort/(1000*60*60*24));
          current.u_total_cost = 100 * dayEffort; 
}


Let me know how that works out.


HI, thanks for your reply



OK, It hasn't worked.



I added some error messages into it to see if I could find the error.   Here is my code now (u_estimated_effort on the form says 9 days - and the day rate says 2)



calculateEstimatedCost(current.u_estimated_effort,current.u_day_rate);



function calculateEstimatedCost(effort, rate){


  gs.addInfoMessage('effort = ' +effort); //1970-01-10 00:00:00


  var dayEffort = (effort/(1000*60*60*24));


  gs.addInfoMessage('dayEffort   = ' +dayEffort); // Came back with NaN????


  gs.addInfoMessage('day rate   = ' +current.u_day_rate); // Came back with   2


      var totalCost = rate * dayEffort;


  gs.addInfoMessage ('effort cost = ' +totalCost); //Came back with NaN


  current.u_estimated_cost = totalCost;


}



The issue must be it coming back with NaN? But Im unsure how I rectify it?



Regards



PS your help is very much appreciated