Skip to page contentSkip to chat
ServiceNow support
    • Community
      Ask questions, give advice, and connect with fellow ServiceNow professionals.
      Developer
      Build, test, and deploy applications
      Documentation
      Find detailed information about ServiceNow products, apps, features, and releases.
      Impact
      Accelerate ROI and amplify your expertise.
      Learning
      Build skills with instructor-led and online training.
      Partner
      Grow your business with promotions, news, and marketing tools
      ServiceNow
      Learn about ServiceNow products & solutions.
      Store
      Download certified apps and integrations that complement ServiceNow.
      Support
      Manage your instances, access self-help, and get technical support.
Setting a variable to be hidden and not mandatory using setDisplay() and setMandatory() does not work at the same time - Support and Troubleshooting
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • Setting a variable to be hidden and not mandatory using setDisplay() and setMandatory() does not work at the same time
KB0718566

Setting a variable to be hidden and not mandatory using setDisplay() and setMandatory() does not work at the same time


2502 Views Last updated : Apr 7, 2024 public Copy Permalink
KB Summary by Now Assist

Issue

Symptoms


When a variable is already mandatory and trying to set it to be not mandatory and hidden as well using g_form.setDisplay() and g_form.setMandatory() it does not work. The variable is simply not hidden.

Release


All releases.

Cause


Caused by the order of code execution.

The g_form.setDisplay() is being invoked first while the variable is still mandatory.

Resolution


Set the variable to not be mandatory first using g_form.setMandatory('variable_name', false) before making it hidden using g_form.setDisplay('variable_name', false).

When the variable is being hidden (using g_form.setDisplay()) but it's still mandatory it would not work because the system does not allow mandatory variables to be hidden as this particular use case does not make sense. The variable remains visible on the form as a result.

For example, instead of writing:

g_form.setDisplay("variable_name", false);
g_form.setMandatory("variable_name", false);

Use this instead:

g_form.setMandatory("variable_name", false);
g_form.setDisplay("variable_name", false);

Additional Information


From general DOM technical standpoint, the following are bad practices:  

  1. If a variable or a field is set as "mandatory" first then it can't be "hidden".
  2. 2. If a variable or a field is set to "hidden" first and then set to "mandatory" => then that element will be visible.

The world works with ServiceNow.

Sign in for more! There's more content available only to authenticated users Sign in for more!
Did this KB article help you?
Did this KB article help you?

How would you rate your Now Support digital experience?

*

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

What can we improve? Please select all that apply.

What are we doing well? Please select all that apply.

Tell us more

*

Do you expect a response from this feedback?

  • Terms and conditions
  • Privacy statement
  • GDPR
  • Cookie policy
  • © 2025 ServiceNow. All rights reserved.