On change client script to calculate the difference in start date and end date and display the result in another field no_of_days in days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2020 03:52 AM
I have two fields start date and end date for my table.I wanted to write an onchange client script such that upon selection of both dates the difference in dates should be auto populated in no-of_days field which is of type integer.
For eg:If start date is 09-09-2020 and end date is 11-09-2020 The no_of_days field should have 3.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2020 04:08 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-23-2020 05:43 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 05:53 AM
Hi Deepika,
please share your scripts here
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-09-2020 06:59 AM
Hi Deepika,
You need to write onChange Client script and Script Include for this.
Please refer below script
Script Include:
Name: diffCalInWfh
var diffCalInWfh = Class.create();
diffCalInWfh.prototype = Object.extendsObject(AbstractAjaxProcessor, {
durCalc: function() {
var i=0;
var start = this.getParameter('sysparm_From');
var end = this.getParameter('sysparm_To');
var days = 0;
start = new GlideDateTime(start);
var daysCountsExcldeWeeknds = 0;
while (start <= end) {
start.addDays(1);
i++;
}
return i;
},
type: 'diffCalInWfh'
onChange Client Script:
var a =g_form.getValue('u_abcd'); // Start Date
var b =g_form.getValue('u_date456'); // End Date
var ajax = new GlideAjax('diffCalInWfh'); //to get the date diff excluding holidays and weekends.
ajax.addParam('sysparm_name', 'durCalc');
ajax.addParam('sysparm_From', a);
ajax.addParam('sysparm_To', b);
ajax.getXML(getResponse);
}
function getResponse(response) {
var days = parseInt(response.responseXML.documentElement.getAttribute('answer'));
g_form.setValue('u_datr4',days); //u_datr4 means no_of_days
}
Note: field name please select your end date field.
Hope this helps!
If you have any more questions, please let me know.
If I have answered your question, please mark my response as correct and helpful.
Thanks,
Sriram
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2020 12:00 AM
Hope you are doing good.
Let me know if I have answered your question.
If so, please mark appropriate answer as correct & helpful to close the thread.
If not, please let us know if you need some more assistance.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader