- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2016 08:54 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2016 01:38 PM
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'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2016 10:36 AM
All of them are of Duration type right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2016 10:38 AM
Yes i Checked the dictionary now and they all are of type duration

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2016 01:38 PM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2016 11:53 AM
This really did work Perfectly, I appreciate it.