Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Cat Client Script for filtering

GMoon
Kilo Sage

Hi all,

I am looking for some support in order to correctly filter a Catalog Item's variables via a Cat Client Script. For example, if I select Production as a value in the Environment variable, I only want Production values to show within the Application Services variable.

The Environment variable is a Multi Choice field, with two values 'Production' and 'Non Production'.

The Application Service variable is a Reference field on the 'cmdb_ci_auto' table, which pulls in various Application Services, they also have values in this table to show whether they are 'Production' or 'Non Production'.

I am having challenges with correct syntax usage and whether I need to glide to the Application Service table. Can someone point me in the right direction please?

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

To filter the list of possible selections in a reference variable, use a reference qualifier on the Application Services variable, not a Catalog Client Script. To incorporate the value of another variable, it would look something like this:

BradBowman_0-1743162979529.png

Where 'used_for' is the name of the field on the referenced table that contains the 'Production' or 'Non Production' value, and 'v_environment' is the name of the Catalog Item variable where that value (which must match exactly) was selected.

View solution in original post

11 REPLIES 11

sunil maddheshi
Tera Guru

@GMoon 

You can write Onchange client script and script include, below is sample example you can modify it for you.

Client script

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

    // Convert the selected values into an array
    var selectedValues = newValue.split(',');

    // Call GlideAjax to fetch valid Application Services
    var ga = new GlideAjax('FilterApplicationServices');
    ga.addParam('sysparm_name', 'getFilteredServices');
    ga.addParam('sysparm_env', selectedValues.join(',')); // Pass selected environments
    ga.getXMLAnswer(function(response) {
        if (response) {
            g_form.setValue('application_service', response); // Set filtered value
        }
    });
}

Script inlcude:

getFilteredServices: function() {
        var envs = this.getParameter('sysparm_env'); // Get selected environments
        var envList = envs.split(','); // Convert to array
        
        var gr = new GlideRecord('cmdb_ci_auto'); // Application Services table
        gr.addQuery('u_environment', 'IN', envList); // Filter based on selected Environment
        gr.query();

        var serviceIds = [];
        while (gr.next()) {
            serviceIds.push(gr.sys_id.toString());
        }

        return serviceIds.join(','); // Return filtered sys_ids
    }

Brad Bowman
Kilo Patron
Kilo Patron

To filter the list of possible selections in a reference variable, use a reference qualifier on the Application Services variable, not a Catalog Client Script. To incorporate the value of another variable, it would look something like this:

BradBowman_0-1743162979529.png

Where 'used_for' is the name of the field on the referenced table that contains the 'Production' or 'Non Production' value, and 'v_environment' is the name of the Catalog Item variable where that value (which must match exactly) was selected.

Hi Brad,

Thanks for the response. This is what I have configured as per your guidance, but the filtering isn't working currently - 

Ref Qual:

GMoon_0-1743165283693.png

Environment field:

GMoon_1-1743165337323.png


Cat Item Variable:

GMoon_2-1743165434070.png

 







You have an errant quote after environment.