Check the user is itil or not in workflow script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 05:57 AM
Hi All
I am using script in workflow ,, but it's not working for me please find-out where is the error
Script:-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 06:13 AM
Hi @1dusjhyahnt
Please try below code and let me if you need any further help -
// Declare the function
function ifScript() {
try {
var ritmGR = new GlideRecord("sc_req_item");
if (!ritmGR.get(current.sys_id)) {
gs.error("Unable to get sc_req_item record with sys_id: " + current.sys_id);
return 'error';
}
var user = ritmGR.variables.requested_for;
// Check if the user has the 'itil' role
var gr = new GlideRecord('sys_user');
if (!gr.get(user)) {
gs.error("Unable to get sys_user record for user: " + user);
return 'error';
}
if (gr.hasRole('itil')) {
return 'yes';
} else {
return 'no';
}
} catch (e) {
gs.error("An error occurred: " + e);
return 'error';
}
}
// Call the function and store the result in the 'answer' variable
var answer = ifScript();
// Log the result to the script log
gs.info('Result: ' + answer);
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 07:10 AM
Hi Tushar , Not Working
Can we connect in LinkedIn :- https://www.linkedin.com/in/dushyant-sirohi-94b055249/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 06:40 AM
Hi @1dusjhyahnt
You can query to the User Role [sys_user_has_role] table, which stores the roles associated with users.
Sample for your case below
var answer = ifScript(); function ifScript() { var gr = new GlideRecord('sys_user_has_role'); gr.addQuery('user', current.variables.requested_for); gr.addQuery('role.name', 'itil'); //gr.addQuery('role', '282bf1fac6112285017366cb5f867469'); //itil role record sys_id gr.query(); return gr.hasNext(); }
Cheers,
Tai Vu