How to make a field2 mandatory based on a choice selected from a dropdown field1

Community Alums
Not applicable

How to make a field2 mandatory based on a choice selected from a dropdown field1.

2 ACCEPTED SOLUTIONS

Community Alums
Not applicable

Hello @Community Alums  ,

Have a nice day!

 

There are multiple ways to achieve this. Few of them are as follow:
1. By using UI Policy 

2. By using Client Script

 

1. By using UI Policy 

To make a field (field2) mandatory based on the selection of a dropdown field (field1) in ServiceNow using a UI policy, follow these simplified steps:

  1. Open the form or table where the fields (field1 and field2) are located in ServiceNow.
  2. Navigate to the UI Policies related list.
  3. Create a new UI policy and give it a name.
  4. Set the "Applies on" field to the field1.
  5. Add a condition to check if field1 has a specific value. For example, if field1 has a dropdown with "High" and "Medium" options, the condition can be field1 == 'High'.
  6. In the UI policy actions section, click on New button. Select field2 and mark it as mandatory.
  7. Save the UI policy and test it.
  8. Verify this: Now, when a user selects "High" in field1, field2 will become mandatory. If any other option is selected in field1, field2 will not be mandatory.

2. By using client script:

To make a field2 mandatory based on a choice selected from a dropdown field1 in ServiceNow using an onChange client script, you can follow these steps:

  1. Create an onChange client script: In ServiceNow, navigate to the form or table where the fields are located. Open the client script section (Client Scripts or UI Scripts) and create a new onChange client script.
  2. Select the table, UI type, type, etc.
  3. Write the script logic: In the client script, write the JavaScript logic to check the value of field1 and make field2 mandatory accordingly. Here's an example:

 

var dropdownField = g_form.getValue('dropdownField');
var mandatoryField = g_form.getControl('mandatoryField');

 if (dropdownField === 'specificChoice') {
            mandatoryField.setAttribute('mandatory', 'true');
 } else {
            mandatoryField.removeAttribute('mandatory');
}

4. Save and Test the behavior by selecting different choices in the dropdownField and observing the mandatory status of mandatoryField. Ensure that the mandatoryField behaves as expected based on the selected choice in dropdownField.

 

 

If this helped you in any way, please hit the like button/mark it helpful. So it will help others to get the correct solution.

 

regards,

Prasad

View solution in original post

AndersBGS
Tera Patron
Tera Patron

HI @Utsav JAISWAL ,

 

The easiest way to make field2 mandatory based on field1 in the same form, is to utilize a UI Policy. By writing the condition when field1 is XXXX, then the UI policy action should be to make filed2 mandatory.

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

View solution in original post

4 REPLIES 4

Community Alums
Not applicable

Hello @Community Alums  ,

Have a nice day!

 

There are multiple ways to achieve this. Few of them are as follow:
1. By using UI Policy 

2. By using Client Script

 

1. By using UI Policy 

To make a field (field2) mandatory based on the selection of a dropdown field (field1) in ServiceNow using a UI policy, follow these simplified steps:

  1. Open the form or table where the fields (field1 and field2) are located in ServiceNow.
  2. Navigate to the UI Policies related list.
  3. Create a new UI policy and give it a name.
  4. Set the "Applies on" field to the field1.
  5. Add a condition to check if field1 has a specific value. For example, if field1 has a dropdown with "High" and "Medium" options, the condition can be field1 == 'High'.
  6. In the UI policy actions section, click on New button. Select field2 and mark it as mandatory.
  7. Save the UI policy and test it.
  8. Verify this: Now, when a user selects "High" in field1, field2 will become mandatory. If any other option is selected in field1, field2 will not be mandatory.

2. By using client script:

To make a field2 mandatory based on a choice selected from a dropdown field1 in ServiceNow using an onChange client script, you can follow these steps:

  1. Create an onChange client script: In ServiceNow, navigate to the form or table where the fields are located. Open the client script section (Client Scripts or UI Scripts) and create a new onChange client script.
  2. Select the table, UI type, type, etc.
  3. Write the script logic: In the client script, write the JavaScript logic to check the value of field1 and make field2 mandatory accordingly. Here's an example:

 

var dropdownField = g_form.getValue('dropdownField');
var mandatoryField = g_form.getControl('mandatoryField');

 if (dropdownField === 'specificChoice') {
            mandatoryField.setAttribute('mandatory', 'true');
 } else {
            mandatoryField.removeAttribute('mandatory');
}

4. Save and Test the behavior by selecting different choices in the dropdownField and observing the mandatory status of mandatoryField. Ensure that the mandatoryField behaves as expected based on the selected choice in dropdownField.

 

 

If this helped you in any way, please hit the like button/mark it helpful. So it will help others to get the correct solution.

 

regards,

Prasad

Community Alums
Not applicable

Thankyou!! @Community Alums 

AndersBGS
Tera Patron
Tera Patron

HI @Utsav JAISWAL ,

 

The easiest way to make field2 mandatory based on field1 in the same form, is to utilize a UI Policy. By writing the condition when field1 is XXXX, then the UI policy action should be to make filed2 mandatory.

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

I would suggest to use UI policy as mentioned by @AndersBGS 

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