Best practices for UI Policies

mfhaciahmetoglu
Mega Sage

Hello all,

 

I am developing some complex forms where many fields disappear and appear, become mandatory and not-mandatory based on the choice of the user on some variables. However, I always notice some seriously inconsistent behavior. For example, fields disappear when we move from one form layout to the other. 

 

I could not figure out the logic behind these. The "order" column does not seem to make much of a difference. 

 

Does anyone know the best practices to tackle the complex, dynamic, changing-with-the-selection-of-the-users forms with multiple UI Policies defined on the same variables?

 

Thank you!

 

Best,

Firat

1 ACCEPTED SOLUTION

Rajdeep Ganguly
Mega Guru


Sure, here are some best practices to tackle complex, dynamic forms with multiple UI Policies defined on the same variables:

1. **UI Policies Order**: The order of UI Policies does matter. The lower the order, the higher the priority. If you have multiple UI Policies acting on the same field, ensure they are ordered correctly.

2. **UI Policy Conditions**: Make sure your UI Policy conditions are not conflicting with each other. If two UI Policies have opposite actions on the same field, it can cause inconsistent behavior.

3. **Use of Scripts**: If your form is very complex, consider using client scripts instead of UI Policies. Client scripts provide more flexibility and control over form behavior.

4. **Group Related Fields**: Group related fields into sections or tabs. This can help manage the visibility and mandatory state of fields more effectively.

5. **Use of Catalog Client Scripts**: For service catalog items, use catalog client scripts to control the behavior of variables based on user selection.

6. **Use of GlideForm (g_form) API**: The GlideForm (g_form) API provides methods to control form fields. You can use these methods in your client scripts to control the behavior of your form fields.

7. **Testing**: Always test your form behavior thoroughly. Make sure all UI Policies and client scripts are working as expected.

8. **Documentation**: Document your form behavior and the logic behind it. This can help you and others understand the form behavior better.

Here is a sample code for a client script that makes a field mandatory based on user selection:

javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == 'some_value') {
g_form.setMandatory('field_name', true);
} else {
g_form.setMandatory('field_name', false);
}
}


In this script, replace 'some_value' with the value that should make the field mandatory, and 'field_name' with the name of your field.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

View solution in original post

10 REPLIES 10

Rajdeep Ganguly
Mega Guru


Sure, here are some best practices to tackle complex, dynamic forms with multiple UI Policies defined on the same variables:

1. **UI Policies Order**: The order of UI Policies does matter. The lower the order, the higher the priority. If you have multiple UI Policies acting on the same field, ensure they are ordered correctly.

2. **UI Policy Conditions**: Make sure your UI Policy conditions are not conflicting with each other. If two UI Policies have opposite actions on the same field, it can cause inconsistent behavior.

3. **Use of Scripts**: If your form is very complex, consider using client scripts instead of UI Policies. Client scripts provide more flexibility and control over form behavior.

4. **Group Related Fields**: Group related fields into sections or tabs. This can help manage the visibility and mandatory state of fields more effectively.

5. **Use of Catalog Client Scripts**: For service catalog items, use catalog client scripts to control the behavior of variables based on user selection.

6. **Use of GlideForm (g_form) API**: The GlideForm (g_form) API provides methods to control form fields. You can use these methods in your client scripts to control the behavior of your form fields.

7. **Testing**: Always test your form behavior thoroughly. Make sure all UI Policies and client scripts are working as expected.

8. **Documentation**: Document your form behavior and the logic behind it. This can help you and others understand the form behavior better.

Here is a sample code for a client script that makes a field mandatory based on user selection:

javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == 'some_value') {
g_form.setMandatory('field_name', true);
} else {
g_form.setMandatory('field_name', false);
}
}


In this script, replace 'some_value' with the value that should make the field mandatory, and 'field_name' with the name of your field.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

Robbie
Kilo Patron
Kilo Patron

Hi @mfhaciahmetoglu,

 

Let me get the ball rolling on this topic.

First and foremost, it's important to understand why using UI policies are considered 'Best Practice'.

Simply put, it's all about the call stack and execution order, UI Policies execute last and importantly after Client Scripts. If there is conflicting logic between a Client Script and a UI Policy, the UI Policy logic applies. (As it runs last)

 

To address your question regarding the Order field/column. If multiple UI Policies exist for the same table, as in your case, use the Order field to set the order of evaluation of the UI Policy Conditions. It's important to understand the order of evaluation is from the lowest number to the highest number (ascending order). 

 

The third aspect of UI Policies which is often overlooked and can have a major impact on behaviour is the 'Reverse if false' checkbox. If this is checked, the opposite action will execute when the Condition field evaluates to false. It is therefore worth evaluating this flag as it is often an aspect I check when reviewing code and behavior.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

mfhaciahmetoglu
Mega Sage

I also found this statement on KB articles of SN:

 

It is suggested that if a field is required to be modified in multiple UI policies with different conditions then this should be implemented using Client script by using if..else condition.

Client script allows better handling and granular control over the elements.

sunwin93
Tera Contributor

Hi @mfhaciahmetoglu ,

I hope you are doing well.

1.For avoiding collision of ui policies and their inconsistent behavior, you should make sure ui policy action should be repetitive.

2.Try to put conditions together and make one ui policy for one field (ui policy action). This will lead you to debug if any issues arises and collision of more ui policies will avoid.

3.Make sure field does not used in client script.

4.Use return if false wisely. 

 

Please mark helpful if find it so.

Thank You

Sanjeet

Lead consultant - InfoBeans

Aniket Chavan
Tera Sage
Tera Sage

Hello @mfhaciahmetoglu ,

No specific best practices are explicitly outlined for UI policies, yet adhering to the following points can enhance the efficiency of UI policy implementation.

  1. Set onLoad to false:

    • If UI Policies don't need to execute on page load, set the onLoad property to false to enhance performance.
  2. Minimize the Number of UI Policies:

    • Use as few UI Policies as possible to avoid prolonged page load times. This helps in maintaining a streamlined and efficient form.
  3. Utilize Condition Builder:

    • Apply conditions using the Condition Builder whenever possible to prevent unnecessary UI Policy scripts from loading, enhancing performance.
  4. Documentation:

    • Use the Short Description and Description fields to document UI Policies thoroughly. Clear documentation aids in understanding and maintaining the logic behind each policy.
  5. Code Comments:

    • Comment your scripts for better clarity and understanding. Well-commented code is essential for ease of maintenance and collaboration.
  6. Avoid Code Duplication:

    • While flagging UI Policies as inheritable can reduce code duplication, be cautious of potential collisions during upgrades. Deactivating, copying, renaming, and then flagging as inheritable can be a cleaner approach, preventing collisions and allowing for manual updates.
  7. Check 'Reverse if False':

    • Evaluate the 'Reverse if False' option based on requirements, but be cautious as it can impact the logic. Additionally, check for conflicts if there are multiple UI Policy actions on the same field/variable.
  8. Use Client Scripts Wisely:

    • Consider using client scripts for complex forms, as they offer more flexibility and control over form behavior. Ensure that there are no conflicts between client scripts and UI Policies.
  9. Group Related Fields:

    • Group related fields into sections or tabs to manage visibility and mandatory states more effectively. This helps in organizing and simplifying the form layout.
  10. Testing and Verification:

    • Thoroughly test UI Policies with various scenarios and interactions to ensure consistent and expected behavior. Use the 'Simulate' feature to validate the form behavior.
  11. Order of UI Policies:

    • Pay attention to the order of UI Policies. Lower order numbers indicate higher priority. Arrange UI Policies to ensure the correct sequence of actions.
  12. Avoid Client Script and UI Policy Conflicts:

    • Ensure that fields modified by UI Policies are not used in client scripts to prevent conflicts. Evaluate the need for using the 'Reverse if False' option to avoid unexpected behavior.
  13. Consider Client Scripts for Multiple Actions:

    • If a field requires multiple actions with different conditions, consider using client scripts with if...else conditions for better handling and control.
  14. Stay Informed and Document:

    • Keep abreast of ServiceNow updates and document your form behavior. Regularly review and update UI Policies to align with changes in form layouts or business processes.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket