- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 03:09 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 03:24 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 06:07 AM
You should follow below best practices to tackle the complex, dynamic scenario:
Dealing with complex forms in ServiceNow that dynamically change based on user input can indeed be challenging, especially when multiple UI policies are involved. Here are some best practices to tackle such scenarios:
Consistency in UI Policies: Ensure that your UI policies are consistent and don't conflict with each other. For instance, if you have multiple UI policies affecting the same field, make sure they work together seamlessly to achieve the desired behavior.
- Order of Execution: Understand the order of execution for UI policies and client scripts. UI policies are executed before client scripts, and the order of UI policies matters. Ensure that the order is set correctly to achieve the desired behavior.
- Avoid Overlapping Conditions: Ensure that the conditions defined in your UI policies do not overlap. Overlapping conditions can lead to unpredictable behavior, where multiple UI policies might be applied simultaneously.
- While UI policies are powerful, they might not cover all scenarios. Consider using client scripts (Client Scripts, UI Scripts, or Script Includes) to handle more complex logic that cannot be achieved through UI policies alone.
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks