Catalog Client Script Help

NMMZ10
Tera Contributor

I'm stuck in this script and cant seem to find the solution.

 

The field 'My Team' should only be visible and mandatory if 'Access Level' is manager. Otherwise, 'My Team' shouldn't even be visible.
How can i solve this? I'm aware its something simple but at this point i feel like I've everything

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@NMMZ10 

you have mixed up your script

update the script as this

Make UI Type - ALL

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

    if (newValue == "manager") { // give the correct value to compare
        g_form.setDisplay('my_team', true);
        g_form.setMandatory('my_team', true);
    } else {
        g_form.setMandatory('my_team', false);
        g_form.setDisplay('my_team', 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

View solution in original post

Shruti
Mega Sage
Mega Sage
function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {
        g_form.setMandatory('my_team', false);
        g_form.setDisplay('my_team', false);
        return;
    }

    if (g_form.getValue('access_level') == 'manager') {
        g_form.setMandatory('my_team', true);
        g_form.setDisplay('my_team', true);
    } else {
        g_form.setMandatory('my_team', false);
        g_form.setDisplay('my_team', false);
    }
}

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@NMMZ10 

you have mixed up your script

update the script as this

Make UI Type - ALL

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

    if (newValue == "manager") { // give the correct value to compare
        g_form.setDisplay('my_team', true);
        g_form.setMandatory('my_team', true);
    } else {
        g_form.setMandatory('my_team', false);
        g_form.setDisplay('my_team', 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

Ashish Parab
Mega Sage

Hello @NMMZ10 ,

 

Instead of creating any client script, you can do this by creating a UI Policy.

Note - My Team variable is hidden.

 

1. Create a new UI policy and add a condition as Access Level is manager.

2. Save the UI Policy.

3. Create a UI policy action under the same record.

4. Select variable name as My Team, and make Mandatory and Visible as True.

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

Shruti
Mega Sage
Mega Sage
function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {
        g_form.setMandatory('my_team', false);
        g_form.setDisplay('my_team', false);
        return;
    }

    if (g_form.getValue('access_level') == 'manager') {
        g_form.setMandatory('my_team', true);
        g_form.setDisplay('my_team', true);
    } else {
        g_form.setMandatory('my_team', false);
        g_form.setDisplay('my_team', false);
    }
}

Robbie
Kilo Patron
Kilo Patron

Hi @NMMZ10,

 

As on onChange script runs onLoad as well, you can easily handle this within an onChange script.

Update the Script Type to onChange and select 'access level' as the variable.

 

Use the following script.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        // Hide the "My Team" variable initially 
        g_form.setMandatory('my_team', false);
        g_form.setDisplay('my_team', false);
    }

    if (newValue == 'manager') {
        // Show and make "My Team" mandatory 
        g_form.setDisplay('my_team', true);
        g_form.setMandatory('my_team', true);
    } else {
        // Hide and make "My Team" not mandatory 
        g_form.setMandatory('my_team', true);
       g_form.setDisplay('my_team', true);
    }

}

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.




Thanks, Robbie