- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2024 09:05 AM - edited ‎01-10-2024 12:29 AM
Hi All,
I have a requirement for the catalog item with two variables, 'start_date' and 'end_date.' The condition is that if the end date is less than 7 business days (Monday to Friday) from the selected start date, the field value should be cleared, and a message should be displayed. I have an onChange client script.
For example, if we select today's date (09/01/2024) as the start date, it should exclude Saturday and Sunday and then calculate 7 days from the start date. Therefore, the end date should be on 18/01/2024.
On Change catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var start_date = g_form.getValue('start_date');
var end_date = g_form.getValue('end_date');
if (start_date && end_date) {
var validateEndDate = new GlideAjax("end_date");
validateEndDate.addParam("sysparm_name", "schedule");
validateEndDate.addParam("sysparm_start", start_date);
validateEndDate.addParam("sysparm_end", end_date);
validateEndDate.getXMLAnswer(function(answer) {
if (answer == "true") {
g_form.clearValue('end_date');
g_form.showFieldMsg('end_date', 'End Date cannot be less than 7 weekdays from the Start Date', 'error');
}
});
}
}
Script Include:
var end_date = Class.create();
end_date.prototype = Object.extendsObject(AbstractAjaxProcessor, {
schedule: function() {
var startDate = this.getParameter("sysparm_start"));
var endDate = new GlideDateTime(this.getParameter("sysparm_end"));
var scheduleId = 'sys_id of the schedule';
var plannedTaskAPI = new SNC.PlannedTaskAPI();
var resp = plannedTaskAPI.calculateDuration(startDate, endDate, scheduleId);
var response = new JSON().decode(resp);
var duration = response.duration;
var dur = duration.split(' ');
if (dur[0] < 7) {
return true;
} else {
return false;
}
},
type: 'end_date'
});
This is the schedule:
Thanks in advance!
@Ankur Bawiskar @Dr Atul G- LNG @Rahul Talreja @Maik Skoddow @Danish Bhairag2 @Pavankumar_1 @Anil Lande @Sainath N @Saurabh Gupta @Tai Vu
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 12:29 AM
var startDate = new GlideDateTime(this.getParameter("sysparm_start"));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2024 09:13 AM
Hi @2022_ServiceNow,
Try to update below script in your script include
var end_date = Class.create();
end_date.prototype = Object.extendsObject(AbstractAjaxProcessor, {
schedule: function() {
var startDate = this.getParameter("sysparm_start");
var endDate = new GlideDateTime(this.getParameter("sysparm_end"));
var scheduleId = 'sys_id_of_your_schedule'; // Replace with the actual sys_id
var plannedTaskAPI = new SNC.PlannedTaskAPI();
var response = plannedTaskAPI.calculateDuration(startDate, endDate, scheduleId);
var response = new JSON().decode(resp);
var duration = response.duration;
var weekdays = parseInt(duration.split(' ')[0]);
if (weekdays < 7) {
return true;
} else {
return false;
}
},
type: 'end_date'
});
Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2024 09:23 AM
Thanks for your response @Anand Kumar P
I have changed, still it's not working. Do we need to modify the schedule?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2024 11:30 PM
Hi,
Did you debug it by adding logs?
What is not working?
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 12:16 AM
Hi @Anil Lande , yes and I got the solution.