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.

Date Time Calculation

littlelingling0
Tera Contributor

Dear Sir,

I had create two fields in incident, one is 'assign date', another is 'resolved date'.

How can I calculate the date difference between 'assign date' and 'resolved date' by using client script? Then put the result into 'date diff' field.

Screen Shot 2017-05-17 at 16.10.10.png

Thanks.

Best regards,

Ling

1 ACCEPTED SOLUTION

drbz
Mega Expert

Hello,


you can use this example from wiki:


http://wiki.servicenow.com/index.php?title=Setting_the_Duration_Field_Value#Calculating_a_duration_a...



Example usage (incident form):


find_real_file.png


Client script:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue === '') {


          return;


    }



  var strt = g_form.getValue('sys_created_on');


  var end = g_form.getValue('opened_at');


 


  var ajax = new GlideAjax('AjaxDurCalc');


    ajax.addParam('sysparm_name','durCalc');


    ajax.addParam('sysparm_strt',strt);


    ajax.addParam('sysparm_end',end);


    ajax.getXMLWait();



  var answer = ajax.getAnswer();



  g_form.addInfoMessage(answer);


  g_form.setValue('u_dur', answer);


   


}



Script include (client callable):



var AjaxDurCalc = Class.create();


AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {


durCalc: function() {


    return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);


  }


});


View solution in original post

5 REPLIES 5

Problem solved. thanks all.