User criteria to restrict by country shown in user location

shembop
Tera Contributor

How would I format a script to restrict to a location's country value based on a users location record.

I am using Country to limit who can see certain things in the Service Catalog, and having trouble getting it to pull the right info.

8 REPLIES 8

Niklas Peterson
Mega Sage
Mega Sage

Hi,



This script can be used as a User Criteria to validate the country of the user's location.



Country('Australia');


function Country(country) {


  var retVal;



  var usr = new GlideRecord('sys_user');


  usr.get(gs.getUserID());



  if (usr.location.country == country) {


          retVal = true;


  } else {


          retVal = false;


  }


  return retVal;


}




Regards


Niklas


Hello,

I find this helpful. How should I modify the script, if I need to set more than 1 country?

Thanks!

Hi,
There are different ways to accomplish that but for 2 countries it could be done like this.

 

Country('Australia','China');

function Country(country,country2) {

 

  var retVal;

 

 

 

  var usr = new GlideRecord('sys_user');

 

  usr.get(gs.getUserID());

 

 

 

  if (usr.location.country == country || country2) {

 

          retVal = true;

 

  } else {

 

          retVal = false;

 

  }

 

  return retVal;

 

}

prashantnagaral
Kilo Guru

Hi Shembop,



You can also try this



answer = false;


var logUser = gs.getUserID();


var gr1 = new GlideRecord('sys_user');


gr1.addQuery('sys_id',logUser);


gr1.query();


if (gr1.next())


{


if (gr1.location.country == "India"){


  return true;


}


  else {


  return false;


}


}



Mark it as answered How to Mark Answers Correct From Inbox View