Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

show or hide on the variable on the basis of choices

RoshaniB
Tera Contributor

show or hide  the variable on the basis of choices can any one help me with this.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@RoshaniB 

it's an easy requirement

something like this in onChange client script

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

    if (newValue == 'value1') {
        g_form.setDisplay('variable1', true);
    } else {
		// make non mandatory and then hide
        g_form.setMandatory('variable1', false);
        g_form.setDisplay('variable1', false);
    }

}

šŸ’” If my response helped, please mark it as correct āœ… and close the thread šŸ”’ā€” this helps future readers find the solution faster! šŸ™

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@RoshaniB 

it's an easy requirement

something like this in onChange client script

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

    if (newValue == 'value1') {
        g_form.setDisplay('variable1', true);
    } else {
		// make non mandatory and then hide
        g_form.setMandatory('variable1', false);
        g_form.setDisplay('variable1', false);
    }

}

šŸ’” If my response helped, please mark it as correct āœ… and close the thread šŸ”’ā€” this helps future readers find the solution faster! šŸ™

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Mohammed8
Mega Sage

Hi @RoshaniB 

Another option without scripting is to use a Catalog UI Policy. You can show or hide a variable by defining a condition based on the user’s selection. In the Catalog UI Policy Actions, choose the variable you want to control and set Visible = true or false. If you also check the ā€œReverse if falseā€ option, the system will automatically apply the opposite visibility when the condition is not met, without the need for  scripting.

 

Here are the documentation links for reference (Service Catalog UI Policies):

https://www.servicenow.com/docs/bundle/zurich-servicenow-platform/page/product/service-catalog-manag... 

https://www.servicenow.com/docs/bundle/zurich-servicenow-platform/page/product/service-catalog-manag... 

 

Thanks and Regards

Mohammed Zakir

@RoshaniB 

Here is the Example for Catalog UI policy

 

1) Create Catalog UI policy. Example:

a) UI policy when monthly_data_allowance choice is 500 MB

 

Mohammed8_1-1765456998064.png

 

2)Create A UI Policy Action example:

 

Mohammed8_2-1765457097204.png

 

3) UI policy Action configuration Example:

The varaible what_was_the_original_phone_number is visible and mandatory only if above condition matches

Mohammed8_0-1765456754232.png

 

Result Verification:

4) Variable is hidden when choice is not 500 MB

Mohammed8_3-1765457256430.png

 

5) Shows variable only when choice condition matches i.e. 500MB

Mohammed8_4-1765457302956.png

 

Thanks and Regards,

Mohammed Zakir