Holidays not considered when using Glideschedule API
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2025 11:41 PM
for a service request i am trying to consider business days only in a specific requirement where i am using schedules of the user to check it. But all i can see is it only considers weekend and not the holidays associated with the schedule. Can anyone help me whats wrong here and why the holidays in schedules are not considered?
Catalog client script and script include is used.
var BusinessDayChecker = Class.create();
BusinessDayChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkBusinessDays: function() {
gs.info("saee 1");
var startDate = this.getParameter('sysparm_start_date');
var employee = this.getParameter('sysparm_employee');
if (!startDate || !employee) {
return 'false';
}
gs.info("saee 2" + employee);
var userGR = new GlideRecord('sys_user');
if (!userGR.get(employee)) {
return 'false';
}
// Get the user's location and schedule
var location = userGR.getValue('location');
if (!location) {
return 'false';
}
gs.info("saee 3 location " + location);
var locGR = new GlideRecord('cmn_location');
if (!locGR.get(location)) {
return 'false';
}
var schedule = locGR.getValue('u_virtusa_schedule');
if (!schedule) {
return 'false';
}
gs.info("saee 4 sched" + schedule);
gs.info("saee 4.5 startDate" + startDate);
var scheduleGR = new GlideSchedule(schedule);
// var scheduleGR = new GlideSchedule();
// scheduleGR.load(schedule);
var startDateTime = new GlideDateTime();
var now = new GlideDateTime();
try {
startDateTime.setDisplayValue(startDate); // Convert the display format
} catch (e) {
gs.error("Failed to parse startDate: " + startDate + ", error: " + e.message);
return 'false';
}
gs.info("saee 5 startDateTime " + startDateTime.getDisplayValue() + "and " + now.getDisplayValue());
// Calculate elapsed business time
// var elapsedBusinessDays = scheduleGR.duration(startDateTime, now).getDayPart();
var elapsedBusinessDays = scheduleGR.duration(now, startDateTime).getDayPart();
gs.info("saee 6 elapsedBusinessDays " + elapsedBusinessDays);
// Check if elapsed time is less than 2 days
return elapsedBusinessDays < 2 ? 'true' : 'false';
},
type: 'BusinessDayChecker'
});
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Fetch field values
var startDate = g_form.getValue('date_and_time'); // Your field name for start date
var employee = g_form.getValue('employee_name_number'); // Reference to the employee field
// alert("Start date changed to: " + startDate);
// alert("Employee is: " + employee);
if (!startDate) {
return;
}
// Parse the Start Date
var startDateObj = new Date(startDate);
var currentDate = new Date();
// Check if Start Date is within 24 hours
var oneDayAhead = new Date();
oneDayAhead.setHours(oneDayAhead.getHours() + 24);
if (startDateObj < oneDayAhead) {
// alert("Start date must be at least 24 hours from now.");
g_form.showFieldMsg('date_and_time', 'Start date must be at least 24 hours from now.', 'error');
g_form.clearValue('date_and_time');
return;
}
// Use GlideAjax to check business days
var ga = new GlideAjax('BusinessDayChecker');
ga.addParam('sysparm_name', 'checkBusinessDays');
ga.addParam('sysparm_start_date', startDate);
ga.addParam('sysparm_employee', employee);
ga.getXMLAnswer(function(response) {
// var isWithin2Days = response.responseXML.documentElement.getAttribute('answer') === 'true';
var isWithin2Days = response;
if (isWithin2Days) {
alert("Hiding refreshments and decorations as the start date is within 2 business days.");
g_form.setMandatory('refreshments', false);
g_form.setMandatory('decorations', false);
g_form.setDisplay('refreshments', false);
g_form.setDisplay('decorations', false);
} else {
g_form.setDisplay('refreshments', true);
g_form.setDisplay('decorations', true);
}
});
}
0 REPLIES 0