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

Moin Kazi
Kilo Sage
Kilo Sage

Hi @JGuerrero0323 ,

 

 

Create a UI policy on the catalog item that provides access to both the variable set fields and the catalog item fields. This approach will allow you to achieve the desired scenario.

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.
 
Thank you!
Moin Kazi
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

Hi Moin,
That approach is not possible because a UI Policy created at the catalog item level cannot directly control fields within a Variable Set. UI Policies only apply to fields at the same level they are defined, so they cannot enforce changes on fields inside a Multi-Row Variable Set. 

Pranav_Thanedar
Mega Sage

Hi @JGuerrero0323 ,

 

I think you have a similar requirement as mentioned in this.

 

https://www.servicenow.com/community/developer-forum/multi-row-variable-set-client-script-to-do-some...

 

If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.
 
Regards,
Pranav T

Sachithananda 1
Tera Contributor

Hi @JGuerrero0323 

  • Create the Catalog Client Script:

    • Go to your catalog item.
    • In the Catalog Item record, scroll down to the Catalog Client Scripts related list and click on New.
    • Select Type: onChange.
    • Choose the Variable for the Yes/No question (e.g., "Required Accessories").
  • Write the Script: Here's a sample script that should do the trick:

     
     

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

    // Get the "Required by Date" variable
    var requiredByDate = g_form.getControl('required_by_date');

    // Check if the "Required Accessories" Yes/No variable is YES
    if (newValue == 'true') { // 'true' for Yes
    g_form.setVisible('required_by_date', true); // Show the date
    g_form.setMandatory('required_by_date', true); // Make it mandatory
    } else {
    g_form.setVisible('required_by_date', false); // Hide the date
    g_form.setMandatory('required_by_date', false); // Make it optional
    }
    }

If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,

Sachi Pai

Ankur Bawiskar
Tera Patron
Tera Patron

@JGuerrero0323 

you have MRVS and based on MRVS variable value outside variable needs to be shown/hidden.

So it's not straight forward.

But what if user adds multiple rows 1 with yes and other with no?

what will happen in that case?

I suggest to add that Date variable within MRVS so that it makes more meaning to the users and they can specify for each row when they require.

Once this is done you can use catalog UI policy which applies on MRVS and on that Yes/No variable and show/hide the other date variable and make it mandatory

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