Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Get the role of the user who requested a catalog item inside the script of a workflow

Smith Johnson
Kilo Sage

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.

find_real_file.png

 

How can I put this logic into the workflow if script?

Thank you in advance,

Smith.

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

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';
}

View solution in original post

4 REPLIES 4

mr18
Tera Guru

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';
}

Yash Agrawal1
Tera Guru

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

Willem
Giga Sage
Giga Sage

You can try:

answer = ifScript();

function ifScript() {
    if (gs.hasRole('snc_internal', 'snc_external')) {
        return 'yes';
    }
    return 'no';
}


Willem
Giga Sage
Giga Sage

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';
}