Set Variable field of Cost Centre on Service Catalog to only show entries matching the User Country

Alex Woolridge
Kilo Expert

Hi,
I am trying to set a dependency on a Variable Set field called Cost Center so it only shows the list of Cost Centers that are available to the requester which matches their Country set against their user profile when they raise a request in the Service Catalog.
We have currently used this option for a specific field on the Incident and also for what items they can see in the User Catalog but we now want to extend that to include the cost center as well. We currently have a high number of cost centers set up so being able to only show the ones valid for the requesters country would be beneficial to the raising of requests.
I have seen a number of other posts about setting dependencies on variable fields in the catalog but none seem to match exactly what I am trying to do.

The current configuration we have is to add a Country field as a reference to the sys_user table and the same field to the user_criteria table but this one is a List. Both these new columns are reference to the core_country table.
Next, in the dictionary for the required incident field, we added a dependent on the Caller User Country field and added in the sys_id to all the choices we need to make dependent.
This then allowed us to have a field on the incident form that is dependent on the User Country selected on their profile.
For the service catalog, we added a user criteria option for the country ie UK only was set to country of United Kingdom and then under the maintain items for the service catalog, we added a "available for" or "not available for" option to each item for this then to only show where it matched the Users Country.
Both of these options work and allow us to have a dependency field and user catalog items only available for certain countrys.
Leading on from this, we now want the Cost Center to only show if the cost center matches the Users Country so we can reduce the list of cost centers they currently see. The difference with Cost Center is it is part of a variable set and not on a standard form as I understand it

Any help would be grately appreciated.
Thanks
Alex

1 ACCEPTED SOLUTION

DirkRedeker
Mega Sage

Hi

I just sat down and created an example for you, which hopefully will solve your issue:

The solution takes the following steps:

- The current user will have a "Location" configures in his "sys_user" record.

- This "Location" record (cmn_location) does have a "country" field,
   which will define the country relevant for searching Cost Centers.

- I will search for all Records in the Cost Center table, where the "Country" in the
  referenced "Location" field is the same as in the user record.

 

The solution is set up for a Catalog Variable of type "Reference" to the "Cost Center" table.

The Reference Field uses an advanced "Reference Qualifier" to limit the shown records.

(I hope this meets your requirements, as I have understood it from your question above).

Steps to reproduce:

 

1) Create a Script Include to be used in the Advanced Reference Qualifier later (in your Catalog Variable)

find_real_file.png

Note: The "Name" field MUST be the same as the name in the Script !!

Here is the Script for copy/paste:

var DirkCountryCostCenterRefQual = Class.create();
DirkCountryCostCenterRefQual.prototype = {
	initialize: function() {
	},

	getData: function(current) {
		var mylocation = gs.getUser().getRecord().getValue('location');

		var gr = new GlideRecord('cmn_location');

		gr.addQuery('sys_id', mylocation);
		gr.query();

		var myCountry = '';

		if (gr.next()) {
			//   gs.info(gr.getDisplayValue('country'));
			myCountry = gr.getValue('country');
		}

		return "location.country=" + myCountry;
	},

	type: 'DirkCountryCostCenterRefQual'
};

 

 

2) Create a Test-Catalog Item

find_real_file.png

The Catalog Item (1) will need a Catalog Variable (2) which I called "cost_center" (3) - see details in the screenshot below.

find_real_file.png

Set the Variable (1) to "Type" = "Reference" and enter the Question (3).

On the "Type Specifications" Tab, set the "Reference" field to the "Cost Center" (cmn_cost_center) table.

Set the "Use reference qualifier" field to "Advanced" (3).

Enter the Value shown in the "Reference qualifier" (4) field as shown in the screenshot below. This will use the Script Include created above to create the filter for the Lookup list.

find_real_file.png

Copy it from here:

javascript:new DirkCountryCostCenterRefQual().getData(current)

 

4) Create Test records

 

The User is configured like shown below:

find_real_file.png

The used Location is set up like:

find_real_file.png

You can see, that the "Country" field is set up as "Mexico" (1).

The following marked three Cost Centers are using Locations, where the Country is set to "Mexico". These will be the Cost Centers shown in the reference List (Catalog Variable) below:

find_real_file.png

 

5) Finally earn your fruits

Test your catalog Item and open the reference List:

find_real_file.png

It will show only the matching and desired records.

 

I hope, this will meet your requirement to make you happy.

Just give it a shot and let me know, please.

 

 

Let me know if that answers your question and mark my answer as correct and helpful.

BR

Dirk

 

View solution in original post

10 REPLIES 10

Hi Alex

I just Cross-checked in Service Portal, and it works as expected:

 

find_real_file.png

I guess, your issue is caused by the double Variable name, and that you are using the "wrong" variable" in your Catalog Item in the Portal.

Please make sure to use a "blank" Catalog Item, with JUST that Variable, and let me know if that works afterwards.

BR

Dirk

Hi

I found the reason, why the Reference Qualifier does not work 🙂

The name of the Script Include must match 100% the Class Name in the Script:

find_real_file.png

And here you have a "typo":

(1) and (2) is written as "CountryCostCentreRefQual" (with "RE")

(3) is written as "CountryCostCenterRefQual"  ("ER").

 

Give that a shot and let me know.

BR

Dirk

 

Hi Dirk,

 

Perfect spot as I completely missed that despite re checking everything 3 or 4 times but this has fixed my issue as working as you originally described and documented. This is working on a new item created and I can view this in both the Portal and ClassicUI

My only other issue is the explanation around the duplicate variable sets when trying to add this new variable to an existing catalog item. 

I have checked a few items and the same message appears on all and each one of these has a variable set but not a variable so I am not sure what you mean by "you have a Variable with the SAME name in a "Variable Set", which seems to be allocated to your catalog Items.You need to make sure, that the variable names are UNIQUE"

Where would we have these being duplicates? 

 

Thanks again for all your help and time on this one

 

Alex

Hi Dirk,

I have figured out the duplicate names for the variables now and will work through how to correct these now

Thanks ever so much for your help on this one

I will now mark as completed

Thanks

Alex

Hi Alex

Thanks for the perfect feedback.

Nice to see, that you could solve it on your side as well.

You are very welcome.

Have fun & BR

Dirk