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?

3 REPLIES 3

Deepak Shaerma
Kilo Sage
Kilo Sage

hi @Kvb Kiran 

no you cant do this via ui policy, because ui policy was designed to handle actions based on conditions of fields directly on the current form's record.

My Custom solution based on your requirement is to create a hidden variable of type to Single Line Text and in the Type Specifications tab, set Hidden to True . 

Modify client scrpt (onChange of var_reference)
current_country is the hidden variable name, chnge it accordingly

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

    g_form.getReference('var_reference', function(cityObj) {
        if (cityObj && cityObj.country) {
      
            var countryDisplayValue = cityObj.country.getDisplayValue() || cityObj.country;
            
      
            g_form.setValue('current_country', countryDisplayValue);
        } else {
            
            g_form.setValue('current_country', '');
        }
    });
}

 now use the new variable value in the ui policy filter conditions


Happy to help! If this resolved your issue, kindly mark it as the correct answer and Helpful and close the thread 🔒 so others can benefit too.
Warm Regards,
Deepak Sharma
Community Rising Star 2025

Ankur Bawiskar
Tera Patron
Tera Patron

@Kvb Kiran 

if reference variable holds exact country like USA, United States etc then this will work

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
        if (window == null) {
            var countryPortalValue = g_form.getDisplayValue('var_reference');
            g_form.setDisplay('var_selectbox', g_form.getValue('var_checkbox') === 'true' && (countryPortalValue == 'United States' || countryPortalValue == 'USA' || countryPortalValue == 'United States (USA)'));
        }
    } catch (ex) {
        var countryNativeValue = g_form.getDisplayBox('var_reference').value;
        g_form.setDisplay('var_selectbox', g_form.getValue('var_checkbox') === 'true' && (countryNativeValue == 'United States' || countryNativeValue == 'USA' || countryNativeValue == 'United States (USA)'));
    }
    //Type appropriate comment here, and begin script below
}

I created blog as well on how to get display value of reference variable in catalog both native + portal

Get Display value of reference variable Service Catalog 

If you are talking about 1 more dot walk level then you will have to use GlideAjax

💡 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