Client script to calculate Difference between 2 fields of type duration( not dates) these are hours, minutes and secons)

bammar
Kilo Sage
Kilo Sage

Hello All,

I specifically need a client script not a business rule to calculate the difference between 2 fields of type duration

So I have field   planned effort   , actual effort and remaining effort. All are of type duration.   Quite simply if i type Planned effort 10, and Actual 5, I need an on Change scrip that will automatically make Remaining effort 5. If tried many things and couldnt get this to work. Any help would be appreciated. I have seen some proposed solutions that need to use a client script.

1 ACCEPTED SOLUTION

Here you go. Create 2 onChnage client scripts and a script include. The following code assumes the u_dur_1, u_du_2,u_dur_3 as the three duration field names.


OnChange script on u_dur_1 field


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


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


  return;


  }


  if(g_form.getValue('u_dur_2')!=''){


  var ga = new GlideAjax('CalculateDuration');


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


  ga.addParam('sysparm_dur1',newValue);


  ga.addParam('sysparm_dur2',g_form.getValue('u_dur_2'));


  ga.getXML(CallBack);


  }



  function CallBack(response) {


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


  g_form.setValue('u_dur_3',answer);


  }


}


OnChange client script on u_dur_2 field:


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


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


  return;


  }


  if(g_form.getValue('u_dur_1')!=''){


  var ga = new GlideAjax('CalculateDuration');


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


  ga.addParam('sysparm_dur2',newValue);


  ga.addParam('sysparm_dur1',g_form.getValue('u_dur_1'));


  ga.getXML(CallBack);


  }



  function CallBack(response) {


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


  g_form.setValue('u_dur_3',answer);


  }



}


Script include:


Name:CalculateDuration


client callable:true


Script:


var CalculateDuration = Class.create();


CalculateDuration.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  durationCalulator: function(){


  var duration = new GlideDuration(this.getParameter('sysparm_dur1') );


  var duration2 = new GlideDuration(this.getParameter('sysparm_dur2') );


  var answer = duration.subtract(duration2);


  return answer.getDurationValue();



  },


  type: 'CalculateDuration'


});


View solution in original post

8 REPLIES 8

Abhinay Erra
Giga Sage

You need to use onChange client scripts on both of these and use GlideAjax for this to pass these values to the server side and return the values.


http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0  


Yes i have tried examples i have found but to no avail. I really am looking for the correct code/ syntax for both the client script and script include. I have never seen an example of a subtraction of two duration fields A - B   = C in the SN community yet.


Abhinay Erra
Giga Sage

I can certainly help you with this. I will get back to you with the script. Before a quick question, In your above example A-B=C, what if the B is greater than A?


Thank you so much. Yes A would be larger - so below when i changed actual to 5, i want Remaining to then show 5.   For a strange reason out of the box Remaining has a Days area but I dont think it should impact us.



find_real_file.png