- 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 09:47 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2016 10:28 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2016 10:31 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2016 10:35 AM