- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 12:28 AM
Hi Community,
In a change_request form, if model is 'Normal' and creation of change request is today, then in this case 'Planned Start Date' should be after 3 business days (weekends must be excluded). If user tries to select time less than 3 days, error should be visible to set any date after 3 days.
Kindly help in achieving this, if you will provide proper script that will be highly appreciated.
Thank You
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 02:03 AM
Hi @prakhar_yadav ,
You can use the condition builder from the UI Policy for this.
And then you can show a message and clear the value via the script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 09:01 AM - edited 05-23-2024 08:46 AM
After re-reading your post, seems a business rule can work, using the logic in the script include. If the calculated date is less that 'current.start_start date', the set it to the calculated date, add message and abort the submission.
(function executeRule(current, previous /*null when async*/) {
var dateValue = current.sys_created_on;
// gs.info("checkPlannedEndDate: called, date = " + dateValue);
var daysToAdd = 3;
var newDate = new GlideDateTime(dateValue);
// Add days
newDate.addDays(daysToAdd);
// Check if start date is in the schedule (8-5 weekdays excluding holidays)
var sched = new GlideSchedule();
sched.load('08fcd0830a0a0b2600079f56b1adb9ae'); // 8-5 weekdays
var startDate = new GlideDateTime(dateValue);
// gs.info('checkPlannedEndDate: checking if start date: ' + startDate + ' is in schedule');
if (!sched.isInSchedule(startDate)) {
// gs.info('checkPlannedEndDate: Start date is not in schedule! Using day-of-week.');
var dayOfWeek = newDate.getDayOfWeekLocalTime();
switch (dayOfWeek) {
case 6:
newDate.addDays(2); //Saturday
break;
case 7:
newDate.addDays(1); // Sunday
break;
}
}
else {
// Check if new date is in the schedule (8-5 weekdays excluding holidays)
// gs.info('addDaysTodate: checking if ' + newDate + ' is in schedule');
while (!sched.isInSchedule(newDate)) {
// gs.info('addDaysTodate: ' + newDate + ' is not in schedule');
newDate.addDays(1);
}
}
if (dateValue < newDate.getValue()) {
gs.addErrorMessage("Planned start must be three days from now.");
current_start_date = newDate;
current.setAbortAction(true);
}
})(current, previous);
Define that on the change_request table, When to run follows:
Simplier script, no schedule needed:
(function executeRule(current, previous /*null when async*/) {
var dateValue = current.sys_created_on;
// gs.info("checkPlannedEndDate: called, date = " + dateValue);
var daysToAdd = 3;
var newDate = new GlideDateTime(dateValue);
// Add days
newDate.addDays(daysToAdd);
// See if new date is on a weekend
var dayOfWeek = newDate.getDayOfWeekLocalTime();
switch (dayOfWeek) {
case 6:
newDate.addDays(2); //Saturday
break;
case 7:
newDate.addDays(1); // Sunday
break;
}
if (dateValue < newDate.getValue()) {
gs.addErrorMessage("Planned start must be three days from now.");
current_start_date = newDate;
current.setAbortAction(true);
}
})(current, previous);