Filter 'Requested for' field based on the selection of the other field

Mari2
Kilo Guru

I’ve created a workflow using an Order Guide and am currently having trouble configuring a filter for the ‘Requested for’ field. I’d really appreciate any insights or suggestions.

 

Page1:

The user selects the order type, with the available options configured in the Order Guide.

 

Mari2_0-1754598802721.png

 

 

Page 2:

  • The top half of the page contains variables loaded from the Catalog Item.
  • The bottom half contains variables from a Variable Set.
  • The ‘Requested_for’ field is a variable from the Variable Set.

 

Mari2_1-1754598802725.png

 

 

Goal:

I want to dynamically control the options shown in the ‘Requested for’ field based on the selection from another field — a Multiple Choice variable named ‘option’, which allows the user to select either ‘Send and Receive’ or ‘Receive only’.

  • If ‘Send and Receive’ is selected: Only display users where ‘employee_number’ field is not empty.
  • If ‘Receive only’ is selected: No filter should be applied – display all users.

 

Mari2_2-1754598802726.png

 

Current Status:

I have a Catalog Client Script and a Script Include to try to apply this logic, but the filter is not working — all user records are being displayed regardless of the selection.

I understand that both ‘onChange’ and ‘onLoad’ scripts are required, but I’m currently testing with just the onChange script.

 

If anyone has encountered a similar situation or has any suggestions, I’d really appreciate your input. Thank you!

 

Catalog client script

Mari2_3-1754598802727.png

 

 

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') return;

 

    // Get selected value from catalog item variable 'options'

    var selectedValue = g_form.getValue('options') || '';

 

    // Fully qualified name for 'requested_for' inside variable set

    var requestedForVar = 'efax_catalog_item_variable_set.requested_for';

 

    // Clear previous value before applying a new filter

    g_form.setValue(requestedForVar, '');

 

    var ga = new GlideAjax('RequestedForFilter');

    ga.addParam('sysparm_name', 'getFilter');

    ga.addParam('sysparm_selected_option', selectedValue);

 

    ga.getXMLAnswer(function(answer) {

        if (!answer) {

            g_form.showFieldMsg(requestedForVar, 'No filter returned. Please contact support.', 'error');

            return;

        }

 

        try {

            // Apply the dynamic reference qualifier to 'requested_for'

            g_form.setReferenceQual(requestedForVar, answer);

            g_form.refreshReference(requestedForVar);

            g_form.showFieldMsg(requestedForVar, 'Filter applied: ' + answer, 'info');

        } catch (e) {

            g_form.showFieldMsg(requestedForVar, 'Error applying filter: ' + e.message, 'error');

        }

    });

}

 

Script Include

Mari2_4-1754598802728.png

 

var RequestedForFilter = Class.create();

RequestedForFilter.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getFilter: function() {

        var selectedValue = this.getParameter('sysparm_selected_option') || '';

        gs.info('[RequestedForFilter] PARAM = ' + selectedValue);

 

        if (selectedValue === 'send_and_receive') {

            this.getResponse().setAnswer('employee_numberISNOTEMPTY');

        } else {

            this.getResponse().setAnswer('active=true');

        }

    }

});

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Mari2 

client script won't help in filtering users.

you need to use advanced reference qualifier on that variable, something like this

Note: Community converts colon : to : so please use : correctly

javascript: var query = ''; if(current.variables.options.toString() == 'send_and_receive') query = 'employee_numberISNOTEMPTY'; query;

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Hi Mari,

In place of the scripts, you can simply use a reference qualifier on the requested_for variable.

javascript: var ret='active=true'; if(current.variables.options === 'send_and_receive'){ret='employee_numberISNOTEMPTY';}ret;

Hi @Brad Bowman ,

Thank you so much for your comment. It did not filter out the users whose employee number is empty.  

Ankur Bawiskar
Tera Patron
Tera Patron

@Mari2 

client script won't help in filtering users.

you need to use advanced reference qualifier on that variable, something like this

Note: Community converts colon : to : so please use : correctly

javascript: var query = ''; if(current.variables.options.toString() == 'send_and_receive') query = 'employee_numberISNOTEMPTY'; query;

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar,

Thank you for your suggestion. It worked like a charm! I really appreciate your help.