Set mandatory using on change client script

Sahar_Kaizer
Tera Contributor

Hi,

I am trying to write on change client script, so that if "state" field changes directly from "new" (1)  to any other status except "in progress" (2), assigned to field will be mandatory on SOW.

 

I tried this:

Sahar_Kaizer_0-1704634820664.png

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }


   if (oldValue == 1 && newValue != 2) {
        g_form.setMandatory('assigned_to', true);
    }else {
        g_form.setMandatory('assigned_to', false);
    }
}
 but it doesnt work...
I would appreciate any help.
 
Thanks, 
Sahar.
8 REPLIES 8

Can you try with UI Policy instead of Client Script.

 

Pavankumar_1
Mega Patron

Hi @Sahar_Kaizer ,

you are code is correct but isTemplate method is missing on you onchange client script

Try below code it will work.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (oldValue == 1 && newValue != 2) {
        g_form.setMandatory('assigned_to', true);
    } else {
        g_form.setMandatory('assigned_to', false);
    }
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

it doesnt work...

Thanks

Hi @Sahar_Kaizer ,

I have tried this it is working for me. can you just use the script which i provided.

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar