- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2020 12:27 AM
I have two fields start_date and end_date.I wanted to writ a client script so that when i select both dates the difference should be populated in another field called no_of_days.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2020 12:57 AM
Hi Deepika,
you will have to write onchange client script on both start_date and end_date fields
Sample script below
Script Include: It should be client callable
var DateCalculations = Class.create();
DateCalculations.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDifference: function(){
var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));
var endDate = new GlideDateTime(this.getParameter('sysparm_endDate'));
var diffSeconds = gs.dateDiff(startDate.getDisplayValue(), endDate.getDisplayValue(), true);
var days = parseInt(diffSeconds)*60*24;
return days;
},
type: 'DateCalculations'
});
Client Script: onChange of Start; similarly you need to have onChange of End
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if(newValue == ''){
g_form.clearValue('no_of_days');
}
if(oldValue != newValue){
var ga = new GlideAjax('DateCalculations');
ga.addParam('sysparm_name', "getDifference");
ga.addParam('sysparm_startDate', g_form.getValue('start_date'));
ga.addParam('sysparm_endDate', g_form.getValue('end_date'));
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setValue('no_of_days', answer); // give proper field name here
}
});
//Type appropriate comment here, and begin script below
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2020 12:57 AM
Hi Deepika,
you will have to write onchange client script on both start_date and end_date fields
Sample script below
Script Include: It should be client callable
var DateCalculations = Class.create();
DateCalculations.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDifference: function(){
var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));
var endDate = new GlideDateTime(this.getParameter('sysparm_endDate'));
var diffSeconds = gs.dateDiff(startDate.getDisplayValue(), endDate.getDisplayValue(), true);
var days = parseInt(diffSeconds)*60*24;
return days;
},
type: 'DateCalculations'
});
Client Script: onChange of Start; similarly you need to have onChange of End
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if(newValue == ''){
g_form.clearValue('no_of_days');
}
if(oldValue != newValue){
var ga = new GlideAjax('DateCalculations');
ga.addParam('sysparm_name', "getDifference");
ga.addParam('sysparm_startDate', g_form.getValue('start_date'));
ga.addParam('sysparm_endDate', g_form.getValue('end_date'));
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setValue('no_of_days', answer); // give proper field name here
}
});
//Type appropriate comment here, and begin script below
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2020 08:17 AM
Thanks Ankur,this worked
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2022 05:08 PM