Onchange client script is not working.

Sangeetha8
Tera Contributor

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

 

     }

}

7 REPLIES 7

Shruti D
Tera Guru

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Sangeetha8 

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.

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

I have missed one part, I need to set state value as previous state if actual start date is less than planned start date

Chavan AP
Tera Guru

@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

 

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*****