Actual End Date onchange client script is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
3 weeks ago
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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;
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@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);
}
}
}
}