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.

Role not showing up in condition in workflow

SuvarnaJadhav
Tera Contributor

I have created a role and added the role to the user, when I check in background script I do see the role for user. But in workflow when I try to get the "Requested For" user's role in condition, it's always evaluating to false. 

1 ACCEPTED SOLUTION

Sreeram Nair
Tera Guru

Workflow conditions in ServiceNow run in the system context, not under a user session, so methods like hasRole() don’t work on referenced users such as “Requested For.” While checking in a background script works since it runs in your own user context, within a workflow the “Requested For” field is just a reference to a sys_user record, not an active GlideUser object.

 

To accurately check a user’s role inside a workflow, you need to query the sys_user_has_role table directly, for example by creating a GlideRecord query that looks for the “Requested For” user and the role name. This ensures the condition correctly evaluates whether that user actually holds the specified role. You can also encapsulate this logic in a reusable Script Include and call it in the workflow condition.

 

If the role was recently added or assigned through a group, you may need to refresh the user’s role cache or log out and back in before the workflow recognizes it.


ɪꜰ ᴍʏ ᴀɴꜱᴡᴇʀ ʜᴀꜱ ʜᴇʟᴘᴇᴅ ᴡɪᴛʜ ʏᴏᴜʀ Qᴜᴇꜱᴛɪᴏɴ, ᴘʟᴇᴀꜱᴇ ᴍᴀʀᴋ ᴍʏ ᴀɴꜱᴡᴇʀ ᴀꜱ ᴛʜᴇ ᴀᴄᴄᴇᴘᴛᴇᴅ ꜱᴏʟᴜᴛɪᴏɴ ᴀɴᴅ ɢɪᴠᴇ ᴀ ᴛʜᴜᴍʙꜱ ᴜᴘ.




ʙᴇꜱᴛ ʀᴇɢᴀʀᴅꜱ


ꜱʀᴇᴇʀᴀᴍ

View solution in original post

5 REPLIES 5

I was able to achieve it by looking at sys_user_has_role, thank you for your response, appreciate it.