Check the user is itil or not in workflow script

1dusjhyahnt
Tera Contributor

Hi All

 

I am using script in workflow ,, but it's not working for me please find-out where is the error

 

Script:- 

 

var answer = ifScript();
 
function ifScript() {
    var ritmGR = new GlideRecord("sc_req_item");
    ritmGR.get(current.sys_id);
    var user = ritmGR.variables.requested_for;
 
 
    var gr = new GlideRecord('sys_user');
 
    if (user.hasRole() == 'itil')
{
        return 'yes';
    } 
else {
        return 'no';
    }
}
3 REPLIES 3

Tushar
Kilo Sage
Kilo Sage

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

Hi Tushar , Not Working 

Can we connect in LinkedIn :-  https://www.linkedin.com/in/dushyant-sirohi-94b055249/ 

Tai Vu
Kilo Patron
Kilo Patron

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