The CreatorCon Call for Content is officially open! Get started here.

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

Hi Uma,



u have to just store addition in one variable and give that value to that third total duration field. see below lines.



var total = current.u_dur1.dateNumericValue() + current.u_dur2.dateNumericValue();


current.u_total_dur.setDateNumericValue(total);



u can similarly do that with client script + script include combo. show me script include n client script if u want further help.



please mark it if it was Helpful.



thanks,


Rushit Patel


aa


I had answered this some time back. Check if that helps



Re: How do I "add" a duration value onto an already existing duration value?


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi uma -



Is this resolved now?


Hi Pradeep, any chance you could please take a look at a similar query on my recent post here:

https://community.servicenow.com/community?id=community_question&sys_id=6aa6a486db0a57c058dcf4621f9619d5

I would be very grateful. Thanks.