help with a client script

Jen11
Tera Expert

Hello, 

I need help with an onChange client script on the TASK level. Once a Task has been created for a certain catalog item, when the State for the TASK changes to 'Closed Complete' I would like a certain field to be mandatory.  Can someone help me with that please? 

8 REPLIES 8

@Jen11 if you are using UI policy, then in the condition builder you can specify the catalog item.

Hi @Jen11 ,

Hope you're doing well!

 

You can add your catalog item condition on an UI policy on Catalog Task(sc_task) Table.

On the condition builder, select show related fields > Requested Item > Item to add filter for a specific catalog item.

Please refer to the attached screenshot, thank you!

 

Best Regards,

Sharif 

@Jen11 

To run it only for particular catalog item do this

you can use a display business rule and get the catalog item name and store in g_scratchpad variable

Then use that in your onChange and onSubmit script

Display business rule: sc_task

Script:

g_scratchpad.catalogItemName = current.request_item.cat_item.name.toString();

Then use that in onChange and onSubmit

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

    if (g_scratchpad.catalogItemName == 'Your Item Name Here') {
        if (g_form.getValue('state') == '3' && g_form.getValue('variables.variableName') == '') {
            g_form.setMandatory('variables.variableName', true);
        }
    }

}
function onSubmit() {

    if (g_scratchpad.catalogItemName == 'Your Item Name Here') {
        if (g_form.getValue('state') == '3' && g_form.getValue('variables.variableName') == '') {
            g_form.setMandatory('variables.variableName', true);
            return 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

J Siva
Tera Sage
Tera Sage

Hi @Jen11 
Use a UI policy to mandate the field based on specific conditions. Please find below a sample UI policy that makes the 'Short Description' field mandatory when the task state is set to 'Closed Complete'. This approach will cover both On Change and On Submit scenarios.

JSiva_0-1751339821162.png

JSiva_1-1751339843064.png

Regards,
Siva