Write a Code for user is itil or not in workflow for if condition ??

1dusjhyahnt
Tera Contributor

Hello User

 

I need a urgent help of How to recognise the user having a ITIL Role or not.

6 REPLIES 6

Harish KM
Kilo Patron

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
}

HarishKM_0-1705481530016.png

 

Regards
Harish

Ahana 01
Tera Expert


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