Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to use reference field's preview record values in UI policy

Kvb Kiran
Tera Expert

Lets assume there are three variables.

1. var_checkbox - true/false

2. var_reference - fetches city locations

3. var_selectbox - Several question choices. By default, variable is hidden.

in a catalog item.

 

When checkbox is true and reference's location's country (lets say USA), then the selectbox should be visible. 

I was thinking of using getReference method to get the country name from the city using catalog client script, not sure if it will return display value from that record or sys_id.

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

    g_form.getReference('var_reference', function(cityObj) {
        g_form.setDisplay(
            'var_selectbox',
            g_form.getValue('var_checkbox') === 'true' && cityObj && cityObj.country && (cityObj.country.name === 'United States' || cityObj.country.name === 'USA' || cityObj.country.name === 'United States (USA)')
        );
    });
}

Is there any way to use via UI policy?

1 ACCEPTED SOLUTION

@Kvb Kiran 

then you can use getReference with callback and dot walk and get Country value and then compare

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ref = g_form.getReference('var_reference', callBackMethod); // your reference variable name
}

function callBackMethod(ref) {
    var country = ref.country.toString();
    g_form.setDisplay('var_selectbox', g_form.getValue('var_checkbox') === 'true' && (country == 'United States' || country == 'USA' || country == 'United States (USA)'));
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

9 REPLIES 9

I am bit confused.
If you can see the selected location.png, a city was selected and there is a button to preview record. When clicking on it, shows the record with fields of it, country.png. I was trying to verify if the previewed record value i.e country is of USA or not. Can we dot walk only for once? 

As this can not be done via UI policy, i was checking if i could achieve just with Catalog client script.

@Kvb Kiran 

then you can use getReference with callback and dot walk and get Country value and then compare

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ref = g_form.getReference('var_reference', callBackMethod); // your reference variable name
}

function callBackMethod(ref) {
    var country = ref.country.toString();
    g_form.setDisplay('var_selectbox', g_form.getValue('var_checkbox') === 'true' && (country == 'United States' || country == 'USA' || country == 'United States (USA)'));
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Can we continuously check for the variables and dynamically make the field visible?

if 1st and 2nd fields are selected, then 3rd is visible. 

if 1st is unchecked, still the 3rd is visible. 

However, when 2nd is unchecked, 3rd is being hidden.

 

@Kvb Kiran 

you will require onchange scripts on other variables also to handle this

I believe I answered your original question.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Anand2799
Tera Guru

Hi @Kvb Kiran,

 

Follow below steps to build this:

Create a catalog variable

Name: country
Hidden: true
Auto‑populate from the reference variable country field (var_reference.country).

 

Create a UI Policy

Condition: country is USA.
UI Policy Action: Set var_selectbox to Visible = true.

Thanks

Anand