Actual End Date onchange client script is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2025 12:09 AM
When change request state is moved to implement state actual start date is mandatory. If actual start date should not be greater than actual end date.
i have written onChange client script for actual start date. But it is not working properly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2025 12:24 AM
try this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var currentState = g_form.getValue('state');
var actualStart = g_form.getValue('work_start');
var actualEnd = g_form.getValue('work_end');
// Only trigger in Implement state (use actual value for 'Implement')
if (currentState == '-1') {
// Ensure both dates are present
if (actualStart && actualEnd) {
// Convert ServiceNow date format to timestamp for accurate comparison
var actualStartNum = new Date(getDateFromFormat(actualStart, g_user_date_time_format)).getTime();
var actualEndNum = new Date(getDateFromFormat(actualEnd, g_user_date_time_format)).getTime();
if (actualStartNum > actualEndNum) {
g_form.showFieldMsg('work_start', 'Actual Start Date cannot be after Actual End Date.', 'error');
g_form.setMandatory('u_justification_end', true);
} else {
g_form.hideFieldMsg('work_start');
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2025 08:20 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2025 12:25 AM
Hi @Saranya K
corrected Script :
function onSubmit() {
var state = g_form.getValue('state'); // Change state
var start = g_form.getValue('work_start'); // Actual start date
var end = g_form.getValue('work_end'); // Actual end date
if (state == '3') { // Implement state sys_id or value, check your dictionary
if (!start) {
g_form.addErrorMessage('Actual Start Date is mandatory in Implement state.');
return false;
}
if (end && new Date(start) > new Date(end)) {
g_form.addErrorMessage('Actual Start Date cannot be greater than Actual End Date.');
return false;
}
}
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2025 08:53 PM
@Saranya K - please check below script , it will help
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 actualEnd = g_form.getValue('work_end');
if (currentState == '-1') {
// Make actual start mandatory
if (!actualStart) {
g_form.setMandatory('work_start', true);
return;
}
// Check if both dates exist
if (actualStart && actualEnd) {
var actualStartDateObj = new Date(actualStart);
var actualEndDateObj = new Date(actualEnd);
if (actualStartDateObj > actualEndDateObj) {
g_form.addErrorMessage('Actual Start Date cannot be greater than Actual End Date.');
g_form.setMandatory('u_justification_end', true);
} else {
g_form.setMandatory('u_justification_end', false);
}
}
}
}