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

PrashantLearnIT
Giga Sage

Hi @JGuerrero0323 

 

Create UI Policy on catalog item level.

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

Hi Prashant,

Creating a UI Policy at the catalog item level wasn’t sufficient. I tried, but it didn’t work as expected. Instead, I used a Client Script to achieve the desired outcome

Najmuddin Mohd
Mega Sage

Hello @JGuerrero0323 ,

Why not add the field in the Variable set itself? 


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


If you add the variable inside as you suggest, you would need to select the date for each new row individually. However, the goal is for the required_by_date to be the same for all items within