Willem
Giga Sage
Giga Sage

Hide Choice field when it has no Options

If you have a dependent field, but not for each selection have options, you can hide the field if no options are available.

For example you have Subcategory, which is dependent on a Selected category. Not for each Categories you have a Subcategory to select. And when you have no Options for subcategory, you want to hide that Subcategory field.

Determine the options; before the Service Portal/Mobile

To hide the subcategory if there are no subcategories available for the Category previously you could use

g_form.getOptions()

However that is not supported in the Service Portal/mobile:

https://community.servicenow.com/community?id=community_question&sys_id=6866c7e1db1cdbc01dcaf3231f96...

 

Also suggested in the Community but also not supported in Service Portal/mobile:

var values = [];

var sel = g_form.getElement('<choicefield>');

for (var i=0, n=sel.options.length;i<n;i++) {

   if (sel.options[i].value) values.push(sel.options[i].value);

}

alert(values.join(","));

Reference:

https://community.servicenow.com/community?id=community_question&sys_id=390f0fa1dbdcdbc01dcaf3231f96...

 

 Supported options

That leaves 2 options:

Option 1: Without Script include/Glide Ajax

Option 1 onChange catalog client script without GlideAjax:

(adjust the client script to only use the setVisible)

CL7

Details can be found here:

https://www.jds.net.au/servicenow-choice-list-dependencies/

A suggested improvement from Kieran:

(adjusted to our use case below)

possibleChoices.query(responseChoices);

function responseChoices(response) {
    if (response.next()) {
        g_form.setVisible('subcategory', true);
    }
    else{
        g_form.setVisible('subcategory', false);
    }
}

 

Option 2: With Script include/Glide Ajax

Or you can do it with an Ajax Call to a Client Callable Script Include:

onChange client script:

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

    var hasSubCategories = new GlideAjax('global.producerHelper');
    hasSubCategories.addParam('sysparm_name', 'hasSubCategories');
    hasSubCategories.addParam('sysparm_category', newValue);
    hasSubCategories.getXMLAnswer(processedHandler);

    function processedHandler(answer) {
        if (answer.length > 0) {
            g_form.setVisible('subcategory', true);
        }
        else {
            g_form.setVisible('subcategory', false);
        }

    }
}

 

 

Client Callable Script Include:

var producerHelper = Class.create();
producerHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    hasSubCategories: function () {
        var choices = [];
        var category = this.getParameter('sysparm_category');
        var gr = new GlideRecord('sys_choice');
        gr.addQuery('dependent_value', category);
        gr.addQuery('name', 'incident');//update table if needed
        gr.addQuery('element', 'subcategory');//replace with your sub category field
        gr.query();
        while (gr.next()) {
            choices.push(gr.value);
        }
        return choices.join(',');
    },

    type: 'producerHelper'
});
Comments
Nires Hallow
Kilo Contributor

Hello Willem,

 

I have a question related to your article.

 

I have a reference variable "XX" in a record producer that has an advanced reference qualifier on it. This means that the user A might see two options, while the user B might see five options.

 

I would like to take the options that each user is able to see in an on_load client script (both effective on platform and portal).

 

How could I do that? I think if I follow your implementation above it will not work, since I can not make a glideRecord on sys_choice, since I don't want to receive all the options but only the ones that the user is able to see.

Thank you.

Ahsan Zafar
Tera Contributor

...

Version history
Last update:
‎01-31-2021 08:56 AM
Updated by: