has role check for an approver

sreejith05
Giga Expert

Hi Team,

 

I have a workflow for a catalog item where in which it will go for requested for manager's approval first once he approves i need to check whether the manager has a specific role if not it will go for second level approval.

I am not sure what i need to put inside the if condition post initial approval the one which i am trying is below and it is not working.

function ifScript() {
if (current.variables.requested_for.manager.hasRole('WFH_Approvers')) {
return 'yes';
}
return 'no';
}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sreejith05 

this will work and no need of query

answer = ifScript();

function ifScript() {

	var roleArray = new global.ArrayUtil().convertArray(gs.getUser().getUserByID(gs.getUserID()).getRoles());
	if(roleArray.indexOf('WFH_Approvers') > -1){
		return  'yes';
	}
	return 'no';

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Try using below with added logs.

 

var mgrrole=current.variables.requested_for.manager;

gs.log('Manager is ',mgrrole);

if (mgrrole.hasRole('WFH_Approvers')) {
return 'yes';
}
return 'no';
}

vinothkumar
Tera Guru

You can try something similar as below

 

var mgrrole=current.variables.requested_for.manager;

 

var m = new GlideRecord('sys_user_has_role');

m.addQuery('user', mgrrole);

m.addQuery('role', sys_id of your role);

m.query();

if(m.next())

{

return 'yes';

}

else 

return 'no';

Ankur Bawiskar
Tera Patron
Tera Patron

@sreejith05 

this will work and no need of query

answer = ifScript();

function ifScript() {

	var roleArray = new global.ArrayUtil().convertArray(gs.getUser().getUserByID(gs.getUserID()).getRoles());
	if(roleArray.indexOf('WFH_Approvers') > -1){
		return  'yes';
	}
	return 'no';

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sharad Patel
Tera Expert

What if user has no roles... will it still work?