Need to prevent user from selecting a past date greater than 7 days but not working as expected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
I have the following requirement, and I am using a script include and client script but I cannot get it to work. What am I doing wrong.?
Emergency Change Type:
- If the selected date is more than 7 days in the past, submission should be prevented.
- If the date is less than or equal to 7 days in the past, submission should be allowed, but a warning message should be displayed.
Normal or Standard Change Type:
- Submission should be blocked if the selected date is any date in the past, with a validation error shown.
Client Script:
Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @Rhonda9 ,
you are using
var daysPastStart = schedule.duration(startDate, now) / (1000 * 60 * 60 * 24);
But, schedule.duration() returns a GlideDuration object, not a number, so dividing it directly gives wrong results unless you call .getNumericValue(). Even if you fix that, duration() only counts business hours in your schedule, not total calendar days, and if you swap the parameters incorrectly it can return negative or zero when you expect a positive days in the past.
First, you need to fix the date difference calculation.
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago - last edited a month ago
var BusinessDaysChecker = Class.create();
BusinessDaysChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkDates: function () {
var changeType = this.getParameter('sysparm_changeType');
var startDate = new GlideDateTime(this.getParameter('sysparm_start'));
var now = new GlideDateTime();
var response = { action: 'allow', message: '' };
// Check if the start date is in the past
if (startDate.getNumericValue() < now.getNumericValue()) {
// Calculate the difference in milliseconds and convert to days
var diffMs = now.getNumericValue() - startDate.getNumericValue();
var daysPast = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (changeType === 'normal' || changeType === 'standard') {
response.action = 'abort';
response.message = 'Planned start date cannot be in the past for Normal or Standard changes.';
} else if (changeType === 'emergency') {
if (daysPast > 7) {
response.action = 'abort';
response.message = 'Emergency change dates cannot be more than 7 days in the past.';
} else { // 0 to 7 days in the past
response.action = 'warn';
response.message = 'Emergency change dates are in the past. This will require special approval.';
}
}
}
return JSON.stringify(response);
},
type: 'BusinessDaysChecker'
});
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Thank you for your response, it doesn't seem to be working for me.