Write a Code for user is itil or not in workflow for if condition ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 12:43 AM
Hello User
I need a urgent help of How to recognise the user having a ITIL Role or not.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 12:52 AM
Hi @1dusjhyahnt see below screenshot of if Activity
script:
answer = ifScript();
function ifScript() {
if (gs.hasRole('itil')) {
return 'yes'; // returns true
}
return 'no'; // returns false
}
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 01:48 AM
Sure, you can check if a user has an ITIL role in ServiceNow by using the following script:
javascript
var userID = 'user_id'; // replace 'user_id' with the actual user id
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', userID);
gr.addQuery('role.name', 'itil');
gr.query();
if (gr.next()) {
gs.print('User has ITIL role');
} else {
gs.print('User does not have ITIL role');
}
Here are the steps:
- Create a new GlideRecord on the 'sys_user_has_role' table.
- Add a query to filter by the user's ID.
- Add another query to filter by the role name 'itil'.
- Run the query.
- If the query returns a record, then the user has the ITIL role. If not, the user does not have the ITIL role.
Please replace 'user_id' with the actual user id you want to check.
nowKB.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 02:19 AM
Sure, here is a sample script that you can use in a workflow to check if a user has the 'itil' role:
javascript
var userID = 'sys_id_of_the_user'; // replace with the sys_id of the user you want to check
var userGR = new GlideRecord('sys_user');
if (userGR.get(userID)) {
var userRoles = userGR.getValue('roles');
if (userRoles.indexOf('itil') > -1) {
// User has the 'itil' role
// Add your code here
} else {
// User does not have the 'itil' role
// Add your code here
}
}
Here is the summary:
- Create a new GlideRecord object for the 'sys_user' table.
- Use the 'get' method to retrieve the user record with the specified sys_id.
- Get the value of the 'roles' field for the user record.
- Use the 'indexOf' method to check if the 'roles' field contains 'itil'.
- If 'indexOf' returns a value greater than -1, the user has the 'itil' role.
- If 'indexOf' returns -1, the user does not have the 'itil' role.
- Add your own code to execute based on whether the user has the 'itil' role or not.
nowKB.com