If script to fetch Employee's country in ServiceNow workflow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2024 11:50 PM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2024 03:37 AM
I modified the code as below the if check is returning to No
var employeeName = current.variables.employee_name;
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('79286ca6dbc9d894ae04cf83159619bb',loca); // sysid of india country
locationGR.query();
if(locationGR.next())
{
res = locationGR.country.toString();
}
gs.info(res);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2024 03:40 AM
locationGR.addQuery('79286ca6dbc9d894ae04cf83159619bb',loca); // sysid of india country
it supposed to be
locationGR.addQuery('sys_id',loca);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2024 03:49 AM
okay how are we checking here if the country is india or no? because I need to same for Jordan country as well
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2024 04:00 AM - edited 09-26-2024 04:00 AM
var emp = current.variables.employee_name;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 01:16 AM
Its giving me parsing error: 'return' outside of function