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 populate values in reference variable based on one choice variable

Amol Pawar
Tera Guru

Hi Experts,

 

I have a requirement. I have two variables.

One is a choice type having two choices, A and B. I have another variable of type Reference, which is getting values from a custom table. I want to populate values in the second variable based on the selection of the first variable, which has two choices.

Since the second variable referred to a custom table, I can't edit that table to create a category where I will add choices A and B of variable one.

 

Is there any other way where I can use any scripting to achieve this?

 

Thanks in advance,

Amol

 

3 REPLIES 3

J Siva
Kilo Patron
Kilo Patron

Hi @Amol Pawar 
You need to have a mapping or links between the choices values and the custom table. Without this, you cannot achieve your requirement.

Regards,
Siva

Robert H
Mega Sage

Hello @Amol Pawar ,

 

You can achieve this through the Reference qualifier on the reference variable.

 

Say for example your choice variable is called "state" and has two options: "open" and "closed".

And in the reference variable you want to show Incidents that are either active or inactive, based on the first choice.

So your reference qualifier would be:

javascript: 'active=' + (current.variables.state == 'closed' ? 'false' : 'true');

Important: replace the : part with an actual colon character (:). 

 

RobertH_0-1745999225457.png

 

Just modify the above example based on your actual scenario.

 

Regards,

Robert

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Amol Pawar 

you will have to use script in script include and call it OR directly have script in advanced ref qualifier

something like this

Note: give correct choice value for A, B and the correct sysIds

javascript: var query='';
if(current.variables.firstVariable == 'choice value A')
query = 'sys_idINsysId1,sysId2'; // populate the 2 records which should be seen
else if(current.variables.firstVariable == 'choice value B')
query = 'sys_idINsysId3,sysId4'; // populate the 2 records which should be seen
query;

If you want to auto populate the reference variable based on option A or B selected then you can write onChange catalog client script on that drop down variable

something like this

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

    if (newValue == 'choice A')
        g_form.setValue('variable2nd', 'sysId1');
    else if (newValue == 'choice B')
        g_form.setValue('variable2nd', 'sysId2');

}

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