- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2020 03:16 AM
Hello all,
I would like to know how I could get the user's role inside the following script.
For example, a user with role "snc_internal" requests a catalog item. I want in the following script to check if the user's role is snc_internal or snc_external. In the first case "yes" will be returned, while in the second case "no" will be returned.
How can I put this logic into the workflow if script?
Thank you in advance,
Smith.
Solved! Go to Solution.
- Labels:
-
Request Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2020 04:22 AM
If you want to have a different user then the logged in user, you can use:
(example below is when workflow is on request item, use the current request requested for, you can change this to use the field you want to use as user)
answer = ifScript();
function ifScript() {
var userObject = gs.getUser().getUserByID(current.request.requested_for);
if (userObject.roles.indexOf('snc_internal') != -1 || userObject.roles.indexOf('snc_external') !=-1) {
return 'yes';
}
return 'no';
}
or:
answer = ifScript();
function ifScript() {
var userObject = gs.getUser().getUserByID(current.request.requested_for);
if (userObject.hasRole('snc_internal','snc_external')) {
return 'yes';
}
return 'no';
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2020 03:57 AM
Hi Smith,
Try below code in the script
var grRole = new GlideRecord('sys_user_has_role');
grRole.addQuery('user', current.requested_for);
grRole.addQuery('role', 'sysID of snc_internal role');
grRole.query();
if(grRole.next()){
return 'yes';
} else {
return 'no';
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2020 04:06 AM
Hello Simth,
If you want to get the logged in user name inside any worlflow script then use the below code.
var user=gs.getUserId();//gs. is an object and getUserId is menthod which hold the sys id of logged in user.
if gs.hasRole('snc_internal'))//hasrole check that the user has that role or not.
return true;
else
return false;
Please keep posted for more help
Regards
Yash Agrawal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2020 04:14 AM
You can try:
answer = ifScript();
function ifScript() {
if (gs.hasRole('snc_internal', 'snc_external')) {
return 'yes';
}
return 'no';
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2020 04:22 AM
If you want to have a different user then the logged in user, you can use:
(example below is when workflow is on request item, use the current request requested for, you can change this to use the field you want to use as user)
answer = ifScript();
function ifScript() {
var userObject = gs.getUser().getUserByID(current.request.requested_for);
if (userObject.roles.indexOf('snc_internal') != -1 || userObject.roles.indexOf('snc_external') !=-1) {
return 'yes';
}
return 'no';
}
or:
answer = ifScript();
function ifScript() {
var userObject = gs.getUser().getUserByID(current.request.requested_for);
if (userObject.hasRole('snc_internal','snc_external')) {
return 'yes';
}
return 'no';
}