How to Show and Make a Variable Mandatory Based on a Yes/No Selection in a Variable Set?

JGuerrero0323
Tera Expert

Hi ServiceNow Community,

I'm trying to achieve the following behaviour in my catalog item:

I have a variable required_by_date, and I want it to be displayed only if the Yes/No variable inside my variable set ("Required Accessories") is selected as YES. Additionally, when it is displayed, it should also be mandatory.

I couldn't find a direct way to achieve this since UI Policies don’t seem to work in this case. I suspect this is because the data is inside a variable set.

Has anyone faced a similar issue? I'd really appreciate a different approach or a workaround to make this work.

I've attached some images for better reference.

Thanks in advance!

1 ACCEPTED SOLUTION

JGuerrero0323
Tera Expert

I created two catalog client scripts to achieve the desired behaviour. It’s a simple approach, but it works for what I need.

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

    if (newValue == 'Yes') {
        if (this) {
            this.cat_g_form.setVisible('required_by_date', true);
            this.cat_g_form.setMandatory('required_by_date', true);
        }
    }
    if (newValue == 'No') {
        if (this) {
            this.cat_g_form.setVisible('required_by_date', false);
            this.cat_g_form.setMandatory('required_by_date', false);
        }
    }
}

Catalog Client Script (onLoad):

function onLoad() {
    this.cat_g_form = g_form;
}

By using this.cat_g_form = g_form in the onLoad script, I ensured that the g_form object is accessible within the Variable Set script. This is necessary because scripts inside a Multi-Row Variable Set don’t have direct access to g_form by default. Assigning it to this.cat_g_form allows the onChange script to properly reference and manipulate the catalog item fields.

 

 

View solution in original post

11 REPLIES 11

Hi Ankur.
If the required_by_date variable is added inside the MRVS as you propose, each row will have its own date selection. However, the goal is to have a single required_by_date for all items, not separate dates for each row.

Since the requirement is to control the visibility of required_by_date based on at least one "Yes" in the MRVS, keeping it outside ensures consistency and avoids users selecting different dates for different rows

JGuerrero0323
Tera Expert

I created two catalog client scripts to achieve the desired behaviour. It’s a simple approach, but it works for what I need.

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

    if (newValue == 'Yes') {
        if (this) {
            this.cat_g_form.setVisible('required_by_date', true);
            this.cat_g_form.setMandatory('required_by_date', true);
        }
    }
    if (newValue == 'No') {
        if (this) {
            this.cat_g_form.setVisible('required_by_date', false);
            this.cat_g_form.setMandatory('required_by_date', false);
        }
    }
}

Catalog Client Script (onLoad):

function onLoad() {
    this.cat_g_form = g_form;
}

By using this.cat_g_form = g_form in the onLoad script, I ensured that the g_form object is accessible within the Variable Set script. This is necessary because scripts inside a Multi-Row Variable Set don’t have direct access to g_form by default. Assigning it to this.cat_g_form allows the onChange script to properly reference and manipulate the catalog item fields.