Actual End Date onchange client script is not working

Saranya K
Tera Contributor

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.

 

var currentState = g_form.getValue('state');
var actualStart = g_form.getValue('work_start');
 var plannedEndDate = g_form.getValue('end_date');
if (currentState ==  '-1') {
     
        var actualStartDateObj = new Date(actualStart);
        var plannedEndDateObj = new Date(plannedEndDate);  
 if (actualStartDateObj > plannedEndDateObj) {
                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);
               
            }
        }
    }
4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Saranya K 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Saranya K 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ravi Gaurav
Giga Sage
Giga Sage

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/

Ajay_Chavan
Kilo Sage

@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);
            }
        }
    }
}

 

 

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****