Restrict Planned end date for 7 days from planned start date for Normal and Standard type of changes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2024 06:53 AM - edited ‎01-16-2024 07:02 AM
Requirement:
When the user enters the end_date, it should calculate the difference between the end_date and the start_date in real time, if the end_date is more than 7 days from the start_date, it will throw a field error message and doesn't let the user save the change_Request form, and when the user tries to save or submit the form, if the end_date is more than 7 days from the start_date, it will throw an error, system stops the user from doing save or submit operation of the form and clears the end_date value and abort action.
This rule will be applied for Change type = Normal and Standard and When the change category will be Build or Decommission.
ScreenShot:
Here is the code for client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var startDate = g_form.getValue('start_date');
var endDate = g_form.getValue('end_date');
if (startDate !== '' && endDate !== '') {
var start = new GlideDateTime(startDate);
var end = new GlideDateTime(endDate);
if (end.getNumericValue() < start.getNumericValue()) {
g_form.addErrorMessage('Planned End Date cannot be before the Planned Start Date.');
g_form.clearValue('end_date');
return;
}
var duration = GlideDateTime.subtract(start, end); // This gives the duration between start and end dates
var days = Math.abs(duration.getDays()); // This gives the duration in days
if (days > 7) {
if (u_qs_approving_manager !== 'Build' || u_qs_approving_manager !== 'Decommission') {
g_form.showFieldMsg('end_date', 'The planned end date should be within 7 days from the planned start date.', 'error');
g_form.addErrorMessage('Planned End Date cannot be more than 7 days after the Planned Start Date.');
g_form.clearValue('end_date');
return false; // This will stop the form from being submitted
} else {
return true;
}
}
}
}
This whole client script is not working, could you please help to fix the code.
- Labels:
-
Change Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2024 07:17 AM
Hi @Kaushik Ghosh ,
You can refer the below articles, Might help to resolve your issue.
You can write a script to check this.Below link will help you out to write the required script.
https://www.servicenow.com/community?id=community_question&sys_id=23020be9db98dbc01dcaf3231f9619c2
https://www.servicenow.com/community/developer-forum/start-date-must-be-7-days-ahead/m-p/1846501
Please appreciate the efforts of community contributors by marking appropriate response as Mark my Answer Helpful or Accept Solution this may help other community users to follow correct solution in future.
Thanks
AJ
Linkedin Profile:- https://www.linkedin.com/in/ajay-kumar-66a91385/