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

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.

RunjayPatel_0-1732181086020.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

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

 

 

@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

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

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

 

Community Alums
Not applicable

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.

Rishi Kendale
Giga Guru

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