Hide and display form section based on conditions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 07:47 AM
There is a table in ServiceNow called 'Approval System'. This table has one field and two form sections. The field on the form is 'Approval Type', which offers two choices: 1. Approval Flow and 2. Catalog Flow. The table also contains two form sections named 1. Oracle System and 2. IT Assets.
In the Oracle System form section, there are three fields: System ID, System Sr. No., and System Type. Both System ID and System Type are mandatory.
In the IT Assets form section, there are fields for Asset Name, Asset Sr. No., and Asset Type. Both Asset Name and Asset Type are mandatory.
If the user selects 'Approval Flow' as the Approval Type, only the Oracle System form section should be visible, while the IT Assets form section should be hidden, and vice versa.
For this I have written a OnChange client script
function onChangeApprovalType() {
var approvalType = g_form.getValue('approval_type');
if (approvalType == '1') { // Assuming '1' corresponds to 'Approval Flow'
g_form.setSectionDisplay('oracle_system', true);
g_form.setSectionDisplay('it_assets', false);
} else if (approvalType == '2') { // Assuming '2' corresponds to 'Catalog Flow'
g_form.setSectionDisplay('oracle_system', false);
g_form.setSectionDisplay('it_assets', true);
}
}
Note: But using the above script its hiding the sections based on the conditions.
Lets say I have selected Approval Flow then it will only show Oracle System form section and Vice Versa. But when go back from Catalog Flow to Approval Flow then both the sections are visible.
How to resolve this Issue?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 08:04 AM
Hello,
Check if section names are correct. Switch the if and else results and see does the it_result even hides in the 'Approval Flow' condition because if not that means probably name of the section is not correct.
Also apply logs and see if the script is entering the if /else if loop correctly.
Please mark my answer as correct based on impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 08:14 AM
Yes, I have checked that the form section names are correct. I have also tried using UI policies, but in the case of UI policies, it works fine when I switch values. However, when I save the record, the form section gets disabled."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 08:35 AM
If the UI polices are working when switching values then write an onsubmit client script which will take care of disabling the correct one based on condition while saving the form.
Please mark my answer as correct based on impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 09:14 AM
Tried using onSubmit Client Script, Not working