If script to fetch Employee's country in ServiceNow workflow

Ankita Gupte
Kilo Sage

We have "employee_name" variable on catalog item, I have a requirement if the user selected in employee name has location.country as India then return value as yes in if script orelse no.

 

I have written below script but its not working, please advice on the same.

 


var employeeName = current.variables.employee_name;

var response = 'no';

// Query the User table to get the user's details
var userGR = new GlideRecord('sys_user');
if (userGR.get('user_name', employeeName)) {

// Check if the user's location is set and if the country is India
var locationGR = new GlideRecord('cmn_location');
if (userGR.location && locationGR.get(userGR.location)) {
if (locationGR.country == 'India') {
response = 'yes';
}

// Return the response
response;

 

10 REPLIES 10

Eshwar Reddy
Kilo Sage

Hi @Ankita Gupte 

Please use below code

var employeeName = 'abel.tuter';

var res = ' ';
var loca= '';

// Query the User table to get the user's details
var userGR = new GlideRecord('sys_user');

userGR.addQuery('user_name',employeeName);
userGR.query();
if(userGR.next())
{
loca = userGR.location.toString();
}
// Check if the user's location is set and if the country is India
var locationGR = new GlideRecord('cmn_location');
locationGR.addQuery('sys_id',loca);
locationGR.query();
if(locationGR.next())
{
res = locationGR.country.toString();
}
gs.info(res);
 

Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution

 

Thanks
Eshwar

Hi Eshwar,

 

Just wanted to understand, why below command is used

var employeeName = 'abel.tuter';

 

var res = ' ';
var loca= '';

 @Ankita Gupte 
I just executed the code above in the background script, using the static variable `employeeName = 'abel.tuter';` for testing purposes.

I declared the variable res as an empty string with var res = ' ';, and also initialized the variable loca as an empty string with var loca = '';   
 

Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution

I actually need this as if script which will return value to yes if its india and no if user is not from india