Snehal2
Kilo Guru

Hi All,

Unfortunately, catalog variables do not have a dependency like a field we have on the forms.

find_real_file.png

The dependent variables can be created in the service catalog in two ways:

A. Using Client Script: Onchange of Category field write following script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '')
	{
		g_form.clearOptions('subcategory');
		g_form.addOption('subcategory', '', '-- None --');
		return;
	}
	if(newValue =='Category 1')
		{
		g_form.clearOptions('subcategory');
		g_form.addOption('subcategory', '', '-- None --');
		g_form.addOption('subcategory','Subcategory11','Subcategory11');
		g_form.addOption('subcategory','Subcategory12','Subcategory12');
		g_form.addOption('subcategory','Subcategory13','Subcategory13');
	}
	else if(newValue =='Category 2')
	{
		g_form.clearOptions('subcategory');
		//g_form.addOption('subcategory', '', '-- None --');
		g_form.addOption('subcategory','Subcategory21','Subcategory21');
		g_form.addOption('subcategory','Subcategory22','Subcategory22');
		g_form.addOption('subcategory','Subcategory23','Subcategory23');
	}
	else if(newValue =='Category 3')
	{
		g_form.clearOptions('subcategory');
		g_form.addOption('subcategory', '', '-- None --');
		g_form.addOption('subcategory','Subcategory31','Subcategory31');
		g_form.addOption('subcategory','Subcategory32','Subcategory32');
		g_form.addOption('subcategory','Subcategory33','Subcategory33');
	}	
}

B. By Creating the table:

The client script cannot be used when we have a large number of categories and subcategories. So in such a case, we have to follow the 2nd way i.e create a table. Following are the steps you need to follow:

find_real_file.png

Now consider bank name as category and Bank choice as subcategory.

STEPS:

1. Create a custom table that contains the values of the category and subcategory. This will act as a dependency table.

find_real_file.png

2. Create Category - type of variable->Select box , and add all the required choices

find_real_file.png

3. Create Subcategory - type of variable->Lookup select box

find_real_file.pngfind_real_file.png4. Add Reference Qualifier and Variable attributes as shown in the above image.

 

Hit like if this post helps you.

 

Regards,

Snehal Madakatti

Comments
Snehal2
Kilo Guru

Slight Correction:

For the below highlighted field - "Lookup Value field", instead of setting it as Bank Name, set it as Bank Choice.

find_real_file.png

shruthi shetty
Mega Explorer

Thank you. This was very helpful.

Snehal2
Kilo Guru

Most Welcome! 🙂

Harish74
Tera Expert

How to create this table?

Harish74
Tera Expert
Thank you very much snehal
Snehal2
Kilo Guru

Most Welcome!

Reddy26
Tera Contributor

Hi,

How to create a dependency table. Can someone reply, please?

Reddy26
Tera Contributor

and why we mentioned Subcategory11 twice here.

 

g_form.addOption('subcategory','Subcategory11','Subcategory11');

Snehal2
Kilo Guru

Coz my display value and backend values have same name.

Refer here for more details  --  Servicenow WIKI

find_real_file.png

Snehal2
Kilo Guru

Its just like creating normal table in servicenow.

Reference - Steps for creating Table

Regards,

Snehal

Jignesh2
Giga Contributor

Very Useful Article. Great Job...

Jignesh2
Giga Contributor

This is Very Useful.

great job..

Snehal2
Kilo Guru

Thank You so much and glad it was helpful!

rojerio
Tera Contributor

Very Helpful Article..

Snehal2
Kilo Guru

Glad that it was helpful for you.

Regards,

Snehal

The SN Nerd
Giga Sage
Giga Sage

I'd advise caution around creating a new table, as this may have license implications. It may count towards your bundled table limit, depending on your licensing agreement.

The safe bet is to extend sys_choice, which is not counted as a custom table.

Snehal2
Kilo Guru

Thanks Paul for the advise. 

Regards,

Snehal

Joe Taylor
Giga Guru

Snehal,

 

This was super helpful!

 

I have a follow-up question:

 

Can you make a subcategory dependent on 2 parent varibles instead of just 1?

 

I have an equipment catalog item.

I created a table with 3 columns:  Purpose, Make, Model

I'd like the user to choose "Purpose".

Then the "Make" list should be filtered down.  (I've done this part already)

I'd like the "Model" choices to be filtered based on Purpose+Make.

Can you do this?

 

Joe

Snehal2
Kilo Guru

Hi Joe,

Glad that this helped you.

I tried for Model which you had mentioned, but w was not able to get the choices as expected. Not sure if we can implement -->"Model" choices to be filtered based on Purpose+Make.

If you find the solution please do let me know as well.

Regards,

Snehal M

Ashley Snyder1
Giga Guru

Why not just point your lookup select box to sys_choice and create a reference qualifier that takes into account the table name, inactive is false, and dependent value of the first lookup select box? If you're already taking advantage of the dependent value on the form (non service catalog) then you should be able to re-use that logic without creating scripts or new tables.

Danny Rhoades
Kilo Guru

Good points, Ashley.  Although, as Paul said above, I believe the best and simplest way is to extend the sys_choice table as that does not have any licensing implications and can be extended up to 1000 times before it does.  In my experience, this is much cleaner and more reliable than simply pointing the variable to the sys_choice table.

Joe Taylor
Giga Guru

How does one go about extending the Sys_choice table exactly?

The SN Nerd
Giga Sage
Giga Sage
  1. Make the sys_choice table extendable via the table record
  2. Create a new table that extends sys_choice
  3. Make the 'Field' dictionary entry non-mandatory
Joe Taylor
Giga Guru

Hi Ashely,

 

I think you have figured out the right approach for me, but I still struggling.

A couple of questions:

1. How do I extend the user table value "Division" to dynamically populate the "Dependent Value" column in the Choice list and also the values of some selected elements from the user table?

(Ideally I'd like to dynamically populate the values for elements:  Department, Title and Location found in the User table all under the Dependent value of "Division".)

 

2.  Why doesn't my reference qualifier pull in my variable from the form?  

javascript:"dependent_value="+current.variables.division

("Division" is a value I pull from the user table in a lookup select box on the form.)

 

Here's a screenshot of some values I manually entered into the Choice Lists.

If I use javascript:"dependent_value="+"Corporate"

everything works.

 

 

find_real_file.png

Ashley Snyder1
Giga Guru

@Joe Taylor please let me know if I am reading your question incorrectly.  Dependent values and reference qualifiers will not pull in data on the form, just limit the choices to select from. 

To dynamically populate variables based upon selection you would either have to use GlideAjax to retrieve the values with a Catalog Client Script.

The current.variables.division method should work, but you'll want to make sure you're narrowing down your search, that may be the issue, try something like:

javascript:'name=sys_user^inactive=false^dependent_value=' + current.variables.division
Joe Taylor
Giga Guru

1.  I would like the Choice table to automatically populate values, elements and labels for the Dependent value "Division" any time a new user is added to the sys_user table.

I don't know how to do this.

(I manually added these values to the choice table in the screenshot above to test my form.)

 

2. No matter what I do, the method "current.variables.divsion" doesn't show me the correct values from the choice table.

I've tried:

javascript: 'name^sys_user^inactive=false^dependent_value=' + current.variables.divison

javascript: 'dependent_value=" + current.variables.division

As a test, I manually just typed in the dependent value as a string and then the pulldown in my form worked.

javascript:"dependent_value="+"Corporate"

What could be going on?

Ashley Snyder1
Giga Guru

I see your example is javascript: 'name^sys_user^inactive=false^dependent_value=' + current.variables.divison

The Caret symbol denotes adding another field and not an operator for it's value, did you try: 

javascript:'name=sys_user^inactive=false^dependent_value=' + current.variables.division
 

 

Joe Taylor
Giga Guru

So this is what my variable looks like now (see below).

My pulldown shows all of the labels of anything in the choice table with a blank "Dependent_value".

So clearly, I'm not gettting the "Divsion" display value I need.

As an experiment I did this in a UI Script:

alert('Current Division is: ' + g_form.getValue('division'));
alert('Current Display Division is: ' + g_form.getDisplayValue('division'));

The first value was the sys_id of division.

The second value was the string "Corporate".

I then updated my Choice table as shown.

Still no luck pulling "division" from the form to use as a reference qualifier.

find_real_file.png

find_real_file.png

TStark
Kilo Sage

Hi Paul,

I tried your recommendation of extending sys_choice and was able to do so, however what do we do after we extend the table. Surely there's more to this as I now have the extended table to choose from but the choice fields available aren't fields that I would use for a Category and Subcategory. I therefore added Category and Subcategory fields (each with their own Dictionary Entries and dependencies) however, the dependency drop down doesn't work.

Thanks,
AJ

The SN Nerd
Giga Sage
Giga Sage

You need to add a reference qualifier that references the extended choice table, including the dependent field in the query, making sure it is the same value as your dependent variable.

Ashley Snyder1
Giga Guru

I suppose it depends on if you're referencing an existing choice list on a table, versus the ability to extend sys_choice and create new choices for your functionality.  For example, working with the Incident table may not be as forgiving as one may be referencing choices created long ago.  My scenario is that the choices already exist, and one can reference sys_choice instead of extending or scripting. But the ability to extend sys_choice is great and I've used it already in scoped applications to provide functionality without impacting licensing.

Ashley Snyder1
Giga Guru

Ah, I did not know Division was returning a sys_id and not a value that would correlate to the value on your sys_choice.

Can you provide more information on what you're trying to do on a high level? I think the comments are floating between different ways to accomplish what Paul had originally posted about, and now a more specific requirement that is dealing with reference fields, dependent values, and populating values from reference fields, in which I and others were providing answers on the basic functionality and original post itself.

TStark
Kilo Sage

I don't have the "Reference Qualifier" option. See screenshot below. I've elevated my Admin Role and no luck. What

 

find_real_file.png

The SN Nerd
Giga Sage
Giga Sage

Set 'Choice table' as your extended choice, and 'Choice field' as the display value (i.e. value).

If you want dependencies, you have to use a variable that supports References, then create a reference qualifier to handle your dependency. These include:

  • Lookup Multiple Choice
  • Lookup Select Box
  • List Collector
TStark
Kilo Sage

Thanks Paul, that helped in regards to the Reference Qualifier. However, I'm still not able to accomplish the overall goal of dependencies through extending the Choice Table. I was able to do it through creating a completely new table, but as you originally mentioned I'd prefer to extend rather than create a new table to avoid license implications. Is there anyway you can provide screenshot instructions on how to accomplish dependencies by extending the choice table similar to what Snehal did in his original post?

Thanks,
AJ

The SN Nerd
Giga Sage
Giga Sage

The dependency is created via the reference qualifier, using the dependent value field on the extended table.

Example of category/subcategory below, for the subcategory variable

javascript:'dependent_value=' + current.variables.category + '^ORdependent_value=^u_choice_set=subcategory^inactive=false^EQ';

u_choice_set is a field i use to reference the variable name so i can use the extended table for multiple variables

TStark
Kilo Sage

I appreciate your assistance Paul, but I'm still not able to achieve dependent fields. I've followed your latest recommendation of using the preexisting Dependent Value field from my extended choice table with the script you've provided and no luck.

There may be other factors that are different between the processes of creating a new table and extending the Choice table to achieve dependent fields that I'm not aware of being the newbie that I am.

Thanks,
AJ

The SN Nerd
Giga Sage
Giga Sage

Can you provide your table specification and reference qualifier code?

TStark
Kilo Sage

Hi Paul,

See below Table specs and the Subcategory variable specs/ref qualifier code. I tried with and without the u_choice_set part of the qualifier code.

find_real_file.png

find_real_file.png

Thanks,
AJ

The SN Nerd
Giga Sage
Giga Sage

Lookup value field should be 'value' and lookup label fields should be 'label' (assuming you are leveraging the existing choice fields - not sure why you wouldn't do this)

See screenshot below, continuing from my example:

find_real_file.png

TStark
Kilo Sage

Thanks Paul for your prompt response. I think that's the problem: I'm not using the existing Choice fields since I don't know which fields to use and how to use them which is why I asked if you could you could provide screenshot instructions on how to accomplish dependencies by extending the choice table similar to what Snehal did in his original post. I assumed the benefit, in this situation, of extending an existing table was to bypass the licensing implications. I'm sorry I forgot to mention that I'm new to ServiceNow.

BTW, I understand you're not able to provide further assistance as no one in the Community is obligated to do so.

Thanks again for your help!
AJ

The SN Nerd
Giga Sage
Giga Sage

If you need further assistance, I would suggest raising a question that addresses this specifically. You'll be much more likely to get help doing that.

Hope you've had success.

Regards,

Paul

Raghav Kapoor
Tera Contributor

Hi Snehal,

 

I tried 1st method, A. Using Client Script: 
but it is not working. I made catalog client script. Here it is:-

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue =='rhel')
{
g_form.clearOptions('os_version');
g_form.addOption('os_version','w1','Windows 2019');
g_form.addOption('os_version','w2','Windows 2016');
g_form.addOption('os_version','w3','Windows 2012 R2');
g_form.addOption('os_version','w4','Windows 2008 R2');
}
else if(newValue =='suse')
{
g_form.clearOptions('os_version');
//g_form.addOption('subcategory', '', '-- None --');
g_form.addOption('os_version','r1','RHEL8');
g_form.addOption('os_version','r2','RHEL7');
g_form.addOption('os_version','r3','Redhat Linux 7.4');
g_form.addOption('os_version','r4','Redhat Linux 6.9');
}
else if(newValue =='windows')
{
g_form.clearOptions('os_version');
//g_form.addOption('subcategory', '', '-- None --');
g_form.addOption('os_version','s1','Oracle Linux 6.7');
g_form.addOption('os_version','s2','Oracle Linux 6.4');
g_form.addOption('os_version','s3','Oracle Linux 5');
}

}

 

It is not working. can anybody help on this.?

 

V_31
Tera Expert

Hi Snehal,

This is a great article but how would i create multiple dependencies e.g variable A, Variable B (dependant on Variable A), Variable C (dependant on Variable B) and Variable D (dependant on Variable C)

Would i just mimic step 3/4?

Kind regards

Not applicable

V,

Look at Ashley and Joe's comments above.  They used sys_choice for these types of things and it's purposefully built for this type of thing.  Once you have your variable options in sys_choice, use a reference or lookup select box and reference qualifier to filter for your options.  Repeat for each variable. Super simple once you get your head wrapped around using the sys_choice table to hold all your options. 

The key is your reference qualifier.  A well defined reference qualifier query works on any table and almost any field. 

Note:  sys_choice is no longer an exempt table, but a great alternative is lookup tables if you need free (under 1000) access to a custom table for this purpose.  But in most cases, sys_choice should be able to handle whatever you throw at it.

V_31
Tera Expert
Hi D, I have tried but it does not look like its possible to do with more than on dependency. Because the requirement requires the first variable to be a select box and the second a lookup select box (works fine) but when you introduce another lookup select box that is dependent on the second lookup select box it doesn't work. I'm hoping I have missed something as this solution is perfect for maintaining choice options. The alternative is to have a catalog client script with script include to check the sys_choice table for dependency but my scripting knowledge is not that good and will have to try and find a similar script to modify. Thanks for your response, if you have any other guidance it will be much appreciated.
Troya1
Mega Guru

Much simpler

Megalithe
Tera Expert

This onChange script dynamically pulls the dependent values from the sys_choice table. In my case had a reference field as a parent for a choice field. The choice didn't update unless a commit happened. This allows it to work without needing to save after the selection.

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	// clear values on change
	g_form.clearOptions('category');

	// set input variable
	var s = g_form.getDisplayBox('business_service').value;

	// query for category values
	var gr = new GlideRecord('sys_choice'); 
	gr.addQuery('table', 'change_request'); 
	gr.addQuery('element', 'category');
	gr.addQuery('dependent_value', s);
	gr.addQuery('inactive', false); 
	gr.query();

	g_form.addOption('category', '' , '-- None --', 0); 
	
	while (gr.next()) { 
		// add code here to process the incident record 
			var v = gr.value;
			var l = gr.label;
			g_form.addOption('category', v, l); //, 1);
	}
}

 

 

 

 

Kruthika BM
Tera Contributor

 

Hello everyone,

 

I have the similar requirement, but based on the multiple selection, the other variable options should be available.

So, I have created my first variable as List collector and the second variable as lookup select box(referencing to custom table). Now, I'm struck to get the variables on my second variable based on the first variable.

We have script include created for it. but, something is wrong with the code. Can anybody please help me to solve this issue. 

Also, for the second variable, I have provided reference qualifier as below - (javascript:getProductForWorkpackage.getProductWorkpackageList(current.variables.select_lines_of_services_that_will_be_provided_under_this_project));)

 

Please find the below code for the script include,

var getProductForWorkpackage = Class.create();
getProductForWorkpackage.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getProductWorkpackageList: function(lineOfService) {
       
var lineOfServiceList = [];
        
gs.print("script include calleed" +lineOfService);
 
var jp = new GlideRecord("u_job_profile");
        jp.addQuery("u_service_lines_l1_product_sub_category_l2", "IN", lineOfService);
        jp.query();
        while(jp.get) {
lineOfServiceList.push(jp.u_service_lines_l1_product_sub_category_l2);
        }
 
        return "u_service_lines_l1_product_sub_category_l2IN"+lineOfServiceList;
    },
    type: 'getProductForWorkpackage'
});

 

Version history
Last update:
‎12-15-2019 06:24 AM
Updated by: