Onchange client script is not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
When change request state is moved to implement state actual start date is mandatory. If actual start date is less than planned start, then throw error message and make justification field mandatory.
I have written onChange client script for actual start date.But it is not working properly,it is making justification field mandatory even the actual start date is not less than planned start date.please fix the issue
var currentState = g_form.getValue('state');
var acutalstart = g_form.getValue('actual_date');var plannedStartDate = g_form.getValue('start_date');
if (currentState == -1){
if (acutalstart < plannedStartDate) {
g_form.addErrorMessage('Actual Start Date cannot be before Planned Start Date.');
g_form.setMandatory('u_justification_end', true);
g_form.setValue('state', '-2');
}
else{
g_form.setMandatory('u_justification_end', false);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited 15 hours ago
Hello @Sangeetha8,
Try with the below script
var currentState = g_form.getValue('state');
var actualStartStr = g_form.getValue('work_start');
var plannedStartStr = g_form.getValue('start_date');
if (currentState == '-1') {
if (actualStartStr && plannedStartStr) {
var actualStart = new Date(actualStartStr);
var plannedStart = new Date(plannedStartStr);
if (actualStart < plannedStart) {
g_form.addErrorMessage('Actual Start Date cannot be before Planned Start Date.');
g_form.setMandatory('u_justification_end', true);
} else {
g_form.setMandatory('u_justification_end', false);
}
}
}
Please Mark Correct ✔️ if this solves your query and also mark Helpful 👍 if you find my response worthy based on the impact.
Regards,
Shruti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
update as this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var currentState = g_form.getValue('state');
var actualStart = g_form.getValue('work_start');
var plannedStartDate = g_form.getValue('start_date');
// Only trigger logic when moving to implement state (replace -1 with correct state value if needed)
if (currentState == '-1') {
var actualStartDateObj = new Date(actualStart);
var plannedStartDateObj = new Date(plannedStartDate);
// Validate that both dates are valid
if (!isNaN(actualStartDateObj.getTime()) && !isNaN(plannedStartDateObj.getTime())) {
if (actualStartDateObj < plannedStartDateObj) {
g_form.addErrorMessage('Actual Start Date cannot be before Planned Start Date.');
g_form.setMandatory('u_justification_end', true);
} 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
yesterday
I have missed one part, I need to set state value as previous state if actual start date is less than planned start date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
@Sangeetha8 - i think issue with date comparison- refer this thread it should resolve the issue: https://www.servicenow.com/community/developer-forum/client-script-date-compare/m-p/1503093