Make Justification field mandatory when change request is out of planned window
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2025 11:56 PM
Hi all,
When Change Request moving to implement and Review State,
if Actual Start Date < Planned Start Date ,
Actual Start Date < Planned End Date,
any one of this true then abort and throw error and make justification field mandatory and set state value to old value .
I have wrote the below on change of state client script . which is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2025 12:04 AM
please share the script here and not the image.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2025 01:03 AM
Hi @Ankur Bawiskar ,
Thanks for replying
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2025 01:07 AM
try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') return;
// Define the state values for 'Implement' and 'Review'.
// (replace 'implementValue' and 'reviewValue' with your actual state values)
var implementValue = 'implement_state_number';
var reviewValue = 'review_state_number';
// Only check at implement or review states
if (newValue != implementValue && newValue != reviewValue) return;
var plannedStart = g_form.getValue('start_date');
var plannedEnd = g_form.getValue('end_date');
var actualStart = g_form.getValue('work_start');
// Helper function to convert GlideDateTime string to JS Date
function parseDateTime(str) {
// Handles empty string
if (!str) return null;
// 'yyyy-MM-dd HH:mm:ss' => 'yyyy-MM-ddTHH:mm:ss'
return new Date(str.replace(' ', 'T'));
}
var dPlannedStart = parseDateTime(plannedStart);
var dPlannedEnd = parseDateTime(plannedEnd);
var dActualStart = parseDateTime(actualStart);
var error = false;
// Actual Start Date < Planned Start Date
if (dActualStart && dPlannedStart && dActualStart < dPlannedStart) {
error = true;
}
// Actual Start Date < Planned End Date
if (dActualStart && dPlannedEnd && dActualStart < dPlannedEnd) {
error = true;
}
if (error) {
// Message to user
g_form.addErrorMessage('Actual Start Date is outside the Planned Window. Please provide justification.');
// Make justification field mandatory (update 'u_justification_end' to your field name if needed)
g_form.setMandatory('u_justification_end', true);
// Optionally, display/focus the justification field:
g_form.setDisplay('u_justification_end', true);
g_form.setFieldMessages('u_justification_end', 'Please provide justification before proceeding.', 'error');
// Reset state to previous value to block transition
setTimeout(function() { g_form.setValue('state', oldValue); }, 100);
// Optional: Focus on justification field
setTimeout(function() { g_form.setFocus('u_justification_end'); }, 300);
} else {
g_form.setMandatory('u_justification_end', false);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2025 12:23 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
