Making catalog task variable mandatory in the sctask form based on sc_task state and another variabl

JPSS
Tera Contributor

Could you please help me to make a variable mandatory in the sc_task form based on sc_task state and another varaible

Please help....

5 REPLIES 5

Runjay Patel
Giga Sage

Hi @JPSS ,

 

you use ui policy to make variable mandatory. Put the condition under when to apply like state is new or work in progress.

 

Accept the solution if it helped.

i tired below code

function onCondition() {

    if (variables.system_do_you_need_help_with == "Optimus")
        g_form.addErrorMessage(a); {
        g_form.setMandatory('variables.close_category_closed_skipped', true);
        g_form.setVisible('variables.close_category_closed_skipped', true);

    }
}
but its throwing the error JPSS_0-1732241521451.png

 

This not ServiceNow code which you have written.

 

create ui policy instead of writing code.

add condition state is new or choose state on which you want to make field mandatory.

Apply ui policy action and choose your variable to make mandatory.

Visit here , I have explained it step by steps 

https://servicenowwithrunjay.com/ui-policy-in-servicenow/

 

Since you want to include a field on the sc_task table and a variable in the Catalog Item, be sure your UI Policy is on the sc_task table, not a Catalog UI Policy in the Catalog Item.  Your script should look more like this:

function onCondition() {
    if (g_form.getValue('system_do_you_need_help_with') == "Optimus")
        g_form.addErrorMessage('error'); 
        g_form.setMandatory('close_category_closed_skipped', true);
        g_form.setVisible('close_category_closed_skipped', true);
    }
}

You might find that the error message remains even when it shouldn't.  If that's an issue, consider using an alert instead, or add a check in either the Execute if true and/or Execute if false scripts to g_form.clearMessages() if conditions are met.  Another perhaps more straightforward approach is to instead use a Client Script on the sc_task table with similar code wrapped in an if statement that is checking the State value.