On Load client script is not working to make checkbox true if another date field is not empty.

abhaysingh98
Tera Contributor

Hi All,

I am new to ServiceNow and facing an issue, I have written an on load client script to make checkbox true if other date field is not empty.

As per my understanding my code is correct but is is not executing.

 

ON Load Script -

 

function onLoad() {
    var dateFieldValue = g_form.getValue('u_due_date_2');
   
    if(dateFieldValue != '') {
        g_form.setValue('u_account_expiry', true);
    }

}
17 REPLIES 17

SN_Learn
Kilo Patron
Kilo Patron

Hi @abhaysingh98 ,

 

Please try the below:

 

function onLoad() {
    var dateFieldValue = g_form.getValue('u_due_date_2');

    if (dateFieldValue != null || dateFieldValue != '') {
        g_form.setValue('u_account_expiry', true);
    }
}

 

 

Please hit like and mark my response as correct if that helps.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

hi @SN_Learn 

 

I tried this code also but it is not working properly for me checkbox is always true no matter if the date field is empty or not.

Hi @abhaysingh98 ,

Try the updated code:

function onLoad() {

    var dateFieldValue = g_form.getValue('u_due_date_2');
 
    if (dateFieldValue) {
        g_form.setValue('u_account_expiry', false);
    } else {
        g_form.setValue('u_account_expiry', true);
    }

}

 

Please hit like and mark my response as correct if that helps.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Jitendra Diwak1
Kilo Sage

Hi @abhaysingh98,

 

Why don't you create UI Policy for this. There is condition field and apply action accordingly. Also check whether it is checked in variable itself. 

 

Please accept my solution if it resolves your issue and thumps 👍 up

 

Thanks

Jitendra 

Please accept my solution if it works for and thumps up.