Catalog Client Script to show variables dynamically

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2024 06:33 PM
Hi Developers,
Please assist, I have added few fields on an existing catalogue item. All these fields will one show when a particular value is selected from a particular drop-down. I have set the fields' Hidden attribute to true to hide and written a script to shown them (setVisible to true) when the selection made. However, the fields are not showing when the selection is made I can't figure out when I might have made a mistake please assist debugging my catalog client scripts. Images and code are shared below:
Code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Define a mapping of categories to their corresponding fields
var fieldMapping = {
'accounts': 'accounts',
'activities': 'activities',
'attachments': 'attachments',
'contacts': 'contacts',
'reporting': 'reporting',
'scheme_alert': 'scheme_alert',
'search_functionality': 'search_functionality',
'tcp': 'tcp'
};
// Get the selected category
var selectedCategory = g_form.getValue('incident_category');
// Loop through the field mapping
for (var category in fieldMapping) {
var fieldName = fieldMapping[category];
if (category === selectedCategory) {
// Set the selected category's field visible and mandatory
g_form.setVisible(fieldName, true);
g_form.setMandatory(fieldName, true);
} else {
// Hide and make other fields not mandatory
g_form.setVisible(fieldName, false);
g_form.setMandatory(fieldName, false);
}
}
}
Catalog Client Script:
Fields Added:
Please be advised that these are not in any formatter, the catalogue item has formatters though.
Regards,
Kamva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 01:26 AM - edited 11-21-2024 01:27 AM
Hi @Kamva ,
Yes but for account rule are clashing, all filed will get hidden except account if match the criteria. Just remove from 1st ui policy and let it be here. If category is not account then it will be hidden else it will display based with mandatory.
I am assuming you want to hide all filed except account if category is account.
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 01:39 AM
@Runjay Patel This one field was just a test, its about 6 fields I wan to hide until a value matching them is selected from the incident category field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 01:45 AM
Then only one ui policy will work, why you have written 2nd ui policy.
I am getting confused now.
You requirement is to hide all field when issue type "Audio issue, chat and so on". if this what you are looking for then 1st ui policy will work.
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2024 08:58 PM
You can achieve this using UI policy, you don't need to write complex script to achieve your requirement. Less script to maintain the better.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 01:53 AM
Hi @Kamva
As suggested by Runjay and John, please keep only 1 UI policy. Maintaining 2 UI policies for similar functionality will conflict each other. By chance some other functionality is required you can add script in the same UI policy.
Thanks,
Rishikesh