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 09:21 AM
Hello,
Can you try the below code:-
function onChangeApprovalType() {
g_form.setSectionDisplay('oracle_system', false);
g_form.setSectionDisplay('it_assets', false);
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);
}
}
Also i assume you have already applied logs and have checked that it is entering both the loops correctly and still the code is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 09:44 AM
As I mentioned, there are a few fields which are mandatory in the form sections. If I removed the mandatory fields and used the script below; it's working fine. However, when I make the fields mandatory and try to use the script below, it's not disabling the form sections.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 10:01 AM
Hi Sharma,
Why don't you try UI policies to make those fields non-mandatory based on either condition and then set the section to disable using the UI policy script?
__PRESENT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 10:03 AM
Yes that is how ServiceNow works so what you need to do is when hiding the section make the field non mandatory and when showing the field make the field mandatory. Control the mandatory fields through the script itself something like below:-
function onChangeApprovalType() {
var approvalType = g_form.getValue('approval_type');
if (approvalType == '1') { // Assuming '1' corresponds to 'Approval Flow'
g_form.setMandatory('field1',false);
g_form.setMandatory('field1',true);
g_form.setSectionDisplay('oracle_system', true);
g_form.setSectionDisplay('it_assets', false);
} else if (approvalType == '2') { // Assuming '2' corresponds to 'Catalog Flow'
g_form.setMandatory('field1',true);
g_form.setMandatory('field1',false);
g_form.setSectionDisplay('oracle_system', false);
g_form.setSectionDisplay('it_assets', true);
}
}
Please mark my answer as correct based on Impact.