Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

bammar
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

All of them are of Duration type right?


Yes i Checked the dictionary now and they all are of type duration


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'


});


This really did work Perfectly, I appreciate it.