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

Mahesh23
Mega Sage

Hi,

You can use UI policy to achieve this. 
First create a UI policy and give your conditions in the When to Apply field.


Once you create the UI Policy, Create a new UI Policy action in the Related Lists. Here you can specify the field that you want to hide and select visible to be False.

Please refer below doc for better understanding 

https://docs.servicenow.com/bundle/sandiego-platform-administration/page/administer/form-administrat...

 

Please mark my response as correct answer or helpful if applicable 

Catalog Categories do not come up as variables that we can set the condition on via UI policy action. I can only set condition on variables that are on the form.

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)

Hi @gracjan ,

Did you get a chance to look into my solution?

 

Thanks,
Mohit Kaushik
Community Rising Star 2022

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)