The catalog item should not be visible to the specific countries.

Archana23
Tera Contributor

The specific catalog item should not be visible for the Mexico, Panama, Dominican Republic , Trinidad & Tobago, Colombia, Venezuela, Peru, Ecuador, Brazil, Bolivia, Uruguay, Chile and Argentina countries. I need help in the user criteria script.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Riya Verma
Kilo Sage
Kilo Sage

Hi @Archana23 ,,

 

 

Hope you are doing great.

 

 

Please try using below script in user criteria:

(function () {
  // Get the country of the user making the request
  var country = gs.getUser().getCompany().getCountry();

  // Define the list of countries where the catalog item should be hidden
  var hiddenCountries = [
    'Mexico',
    'Panama',
    'Dominican Republic',
    'Trinidad & Tobago',
    'Colombia',
    'Venezuela',
    'Peru',
    'Ecuador',
    'Brazil',
    'Bolivia',
    'Uruguay',
    'Chile',
    'Argentina'
  ];

  // Check if the user's country is in the list of hidden countries
  if (hiddenCountries.indexOf(country) !== -1) {
    return false; // Hide the catalog item
  } else {
    return true; // Show the catalog item
  }
})();
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

View solution in original post

8 REPLIES 8

@Archana23 

in system property store the sysIds as comma separated value like sysId1,sysId2,sysId3 etc

Also avoid giving space in the name of system property

then update script as this

var location_sys_id = '';
var user_id = gs.getUserID();
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', user_id);
user.query();
if (user.next()) {
	location_sys_id = user.location;
}


var final_parent = '';

do {
	var loc = new GlideRecord('cmn_location');
	loc.addQuery('sys_id', location_sys_id);
	loc.query();
	if (loc.next()) {
		var parentloc = loc.parent; // returns parent sysid
		var parentname = loc.getDisplayValue('parent');
		var userlocname = loc.name; // returns parent name
		var location_type = loc.cmn_location_type;
		if (location_type == 'region') {
			final_parent = loc.sys_id;
			break;
		}
	}
	final_parent = loc.parent;
}

while (parentname == userlocname);
var ans1 = false;
var prop = gs.getProperty('propertyName');
if (prop.indexOf(final_parent) > -1) {
	ans1 = true;
}
answer = ans1;

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

@Archana23 

As per best practices you should not use gs.getUser()

instead use user_id which gives you logged in user's sysId and then query user table to get the country

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Riya Verma
Kilo Sage
Kilo Sage

Hi @Archana23 ,,

 

 

Hope you are doing great.

 

 

Please try using below script in user criteria:

(function () {
  // Get the country of the user making the request
  var country = gs.getUser().getCompany().getCountry();

  // Define the list of countries where the catalog item should be hidden
  var hiddenCountries = [
    'Mexico',
    'Panama',
    'Dominican Republic',
    'Trinidad & Tobago',
    'Colombia',
    'Venezuela',
    'Peru',
    'Ecuador',
    'Brazil',
    'Bolivia',
    'Uruguay',
    'Chile',
    'Argentina'
  ];

  // Check if the user's country is in the list of hidden countries
  if (hiddenCountries.indexOf(country) !== -1) {
    return false; // Hide the catalog item
  } else {
    return true; // Show the catalog item
  }
})();
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Thanks..It worked