hide reference field based on catalog category or catalog item in variable set

gracjan
Tera Guru

Is there a way to hide a variable based on the Catalog category selected in a variables set? I have a variable set which is populated to various forms which needs to hide a variable (reference field). I would like to hide the variable when Software  category is selected. When I select "No" below, I get two reference fields. Is there a way to make the variable to be hidden if it is under Software category. Note, all these variables are under a variable set.find_real_file.png 

1 ACCEPTED SOLUTION

Mohit Kaushik
Mega Sage
Mega Sage

Hi @gracjan 

You can achieve this functionality by creating an onChange catalog client script on your variable set and a script include. It can run onchange your choice variable and then it should work as you want it.

Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var sys_id = g_form.getUniqueValue();
    var category = new GlideAjax('TestScriptInclude');
    category.addParam('sysparm_name', 'getCatalogCategory');
    category.addParam('sysparm_item', sys_id);
    category.getXML(ResponseFunction);

    function ResponseFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
          g_form.setDisplay('reference field name',false);
        } else {
            // do what you want to do
        }
    }
}

 

Script include should be client callable.

var TestScriptInclude = Class.create();
TestScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCatalogCategory: function() {
        var catalog = this.getParameter('sysparm_item');
        var catItem = new GlideRecord('sc_cat_item');
        catItem.addQuery('sys_id', catalog);
        catItem.query();
        if (catItem.next()) {
            var cat = catItem.category;
        }
        if (cat == 'sys id of software category')
            return true;
        else
            return false;
    },
    type: 'TestScriptInclude'
});

 

Please mark this as correct and helpful if it resolved the query or lead you in right direction.

Thanks,
Mohit Kaushik
Community Rising Star 2022

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

View solution in original post

5 REPLIES 5

Thank you. It is helpfull.