Adding two duration fields

uma17
Tera Guru

Hi,

Can anyone let me know how to add two or more duration fields.

Thanks

Uma

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Uma,



After spending some good amount of time on your req, Here is the working solution.


Create a client script :


var dur1 = g_form.getValue('u_dur1');


var dur2 = g_form.getValue('u_dur2');


var ga = new GlideAjax('HelloWorld');


ga.addParam('sysparm_name','helloWorld');


ga.addParam('sysparm_user_dur1', dur1);


ga.addParam('sysparm_user_dur2', dur2);


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);


g_form.setValue('u_dur3',answer);


}


Script Include :


Name : HelloWorld


Client Callable : True


var HelloWorld = Class.create();


HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  helloWorld: function() {


  var result = 'true';


  var dur1 = this.getParameter('sysparm_user_dur1');


  var dur2 = this.getParameter('sysparm_user_dur2');


  var x = new GlideDuration(dur1).getNumericValue();


  var y = new GlideDuration(dur2).getNumericValue();


  var total = x + y;


  var tota11 = new GlideDuration();


  tota11.setNumericValue(total);




  return tota11.getDurationValue();



  },



  _privateFunction: function() { // this function is not client callable



}




});



Please change the column name and naming conventions after round of testing.


I have tested it and it is working fine at my end. Please let me know the outcome.


View solution in original post

14 REPLIES 14

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Uma,



Please check the below thread for more info.


How to add two duration fields


Hi Pradeep,



I had gone through this before and tried the below code, mine is an onChange() script , and I have added the below code in Script includes.



var dur = new DurationCalculator();


dur =gr.duration.dateNumericValue() + gr.duration.dateNumericValue();


gr.duration.setDateNumericValue(dur);



But it is returning NaN



Since I am passing the parameter values , I am not sure whether to use gr.duration



So I tried



dur = param1.dateNumericValue() + param2.dateNumericValue();


return dur;




this is also returning NaN



Thanks


Uma


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Uma,



Try this on business rule. If not use client script + GlideAjax.


Please let me know if you have any questions.


Hi Pradeep,



As mentioned in my   above reply I have used GlideAjax in client script & wrote the above code in script includes but it is returning NaN.



Thanks


Uma