Catalog Client Script to show variables dynamically

Kamva
Giga Guru

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:
Screenshot 2024-11-21 at 04.28.15.png

Fields Added:
Screenshot 2024-11-21 at 04.31.28.png

 

Please be advised that these are not in any formatter, the catalogue item has formatters though.

 

Regards,

Kamva

9 REPLIES 9

Runjay Patel
Giga Sage

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.

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:
Screenshot 2024-11-21 at 10.16.37.pngScreenshot 2024-11-21 at 10.16.52.png

 

Catalog UI Policy to show variables:
Screenshot 2024-11-21 at 10.18.35.png

Hi @Kamva ,

 

Remove the account from 1st UI policy and then try, it will work.

RunjayPatel_0-1732179254746.png

 

 

-------------------------------------------------------------------------

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

-------------------------------------------------------------------------

 

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