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.

I am using this script in workflow , but i'm getting an error please check it?

1dusjhyahnt
Tera Contributor

Script :

 

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);
5 REPLIES 5

Danish Bhairag2
Tera Sage

Hi @1dusjhyahnt ,

 

What is the error which u getting?

Any snip or can u paste the error here please.

 

Thanks,

Danish

 

nirwan_ritik
Tera Contributor

Hi Dushyant,

Could you please let me know your business requirement here.

Thanks.

nirwan_ritik
Tera Contributor
Please replace your code with the below code. Mark helpful if this helped. 
Code:
function ifScript() {
 
        var user = ritmGR.variables.requested_for;
 
        var gr = new GlideRecord('sys_user');
gr.get('sys_id', user);            
 
        if (gr.hasRole('itil')) {
            return 'yes';
        } else {
            return 'no';
        }
}
 

Danish Bhairag2
Tera Sage

Hi @1dusjhyahnt ,

 

Please try below code:

 

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';
        }else{

            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);

 

Thanks,

Danish