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

 

     }

}

8 REPLIES 8

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

rohitjami20
Tera Contributor

Hi @Sangeetha8 ,

In Change Request Table, The Actual Start Date field back-end name is "work_start", not "actual_start".
Also, the Justification field is not a custom field, so back-end field name will be "justification", not "u_justification_end"


Try the below script:
 

var currentState = g_form.getValue('state');

    var acutalstart = g_form.getValue('work_start');
    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('justification', true);
            g_form.setValue('state', '-2');
        } else {
            g_form.setMandatory('justification', false);
        }

    }

 
Thank You

Regards,
Rohit

t_sadahisa
Giga Guru

Hello @Sangeetha8 

 

I think the column name might be wrong.
In the script you wrote, it's “actual_date,” but “work_start” seems to be the correct one.

 

var currentState = g_form.getValue('state');
var acutalstart = g_form.getValue('work_start');
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);
    }
}

 

rohitjami20
Tera Contributor

Hi @Sangeetha8 

In Change Request Table, 
The Field names are incorrect in the code
i.e. Actual Start Date ('work_start'), but mentioned as 'start_date'
also Justification field ('justification'), but you have mentioned it as 'u_justification_end'

Try the below code: 

    var currentState = g_form.getValue('state');

    var acutalstart = g_form.getValue('work_start');
    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('justification', true);
            g_form.setValue('state', '-2');
        } else {
            g_form.setMandatory('justification', false);
        }

    }

Thank you

 

Regards,
Rohit