Announcing the Global SNUG Board of Directors. Learn more here

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

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

 

Good Morning Dirk,

 

Really appreciate the time you have spent looking into this for me and the detailed description of how to replicate. 

 

I have followed the steps this morning and they don't seem to be limiting the Cost Center to the Users country. I have repeated the steps numerous times so not sure if this is an issue with a local set up of our system or something I may of missed. I have attached screen shots of this set up locally, if you could have a look and see if this is correct as you would expect it to work. 

When I run the catalog and look at the new item, it shows all Cost Centers, not just the ones limited to the user country

 

Also, when I try to add this to an existing item that is set up, it gives the following error so not sure how viable this is for our set up. 

find_real_file.png

Thanks in advance

Alex

HI

Sure, I will have a look. I am quite sure that it works, because I just set it up for this purpose and tested it.

Let me some minutes to have a look at your documentation.

I'll come back asap.

BR

Dirk

Hi Alex

I just reviewed your Word File and at least I can see, that I understood your question correctly and my steps towards the solution are the right direction.

First of all, I want to mention, that the screenshot above indicates, that 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

Second, as I found out, you tested your solution in the Portal, where I tested that in the Classic UI, but I will cross-check that - I think (hope) it should work the same; in Backend and Frontend (Portal).

Maybe you do NOT see/use the NEW Variable that you just created when accessing your Test Item in the Portal. Because of the error message above states, that you already have another variable with the same name attached via "Variable Set".

Maybe you just create another new Catalog Item with NO other Variable then your test "Cost Center" variable.

Can you confirm that test above?

Let me know if that solves your issue, while I start testing it in the Portal.

BR

Dirk