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-20-2024 06:46 PM
Hi @Kamva ,
You should not do hidden at attribute level, if you do then client script code won’t work.
remove the checkbox and then apply either client script or ui policy.
Accept the solution if it helped.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 12:20 AM
Thank @Runjay Patel, I have used a catalog ui policy to hide these variables as per your suggestions. They do not display as per requirement but I can't show them on selections, please check the attached images of the catalog ui policies. Please assist.
Catalog UI Policy hiding variables:
Catalog UI Policy to show variables:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 12:53 AM - edited 11-21-2024 12:54 AM
Hi @Kamva ,
Remove the account from 1st UI policy and then try, it 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-21-2024 12:56 AM
Hi @Runjay Patel,
I want hide all of them, on the second UI Policy I was trying to show Accounts only when Accounts is selected on the Incident Category