Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Can I have redirect user if they have a specific role

Wyatt Fudal1
Tera Guru

Hello Developer community,

 

I ran into an issue trying to set if conditions in a sc_cat_item_producer script. I have a redirect working to direct the submitter to the task record. I want to make a if and else if in the script. If the user has the contract admin role to be redirected to the task record and else if false to redirect to my thank you page. I believe my code is incorrect. 

 

Thank you for Assistance 

Script

var roles = userRoles.indexOf('contract_admin');
if(roles == true){
    producer.portal_redirect = 'u_contract_master.do?sys_id=' + current.sys_id;
    } else if (roles == false) {
        producer.portal_redirect = "/cca?id=cca_thankyou";
    }
 
I also tried
var hascontractadminRole = userRoles.indexOf('contract_admin');
 
if(hascontractadminRole == 'true'){
producer.portal_redirect = 'u_contract_master.do?sys_id=' + current.sys_id;
} else {
producer.portal_redirect = "/cca?id=cca_thankyou";
}

 

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Simplest would be to use the gs.hasRole() method:

 

if (gs.hasRole("contract_admin"))){  //hasRole will return true if the current user has the specified Role
    producer.portal_redirect = 'u_contract_master.do?sys_id=' + current.sys_id;
} else {
    producer.portal_redirect = "/cca?id=cca_thankyou";
}

 

You don't actually specify where the userRoles variable is from or gets its data.

View solution in original post

2 REPLIES 2

Josh_H
Giga Guru

Hey @Wyatt Fudal1,

 

Please take a look at the following script. I believe your use of 'indexOf' was incorrect. This should fix it.

 

Cheers,

Josh

 

If you found this helpful please give a thumbs up OR mark it as the solution. Thank you!

 

var hasContractAdminRole = userRoles.indexOf('contract_admin') !== -1; // should be true if the role is found 

if(hasContractAdminRole) {
    producer.portal_redirect = 'u_contract_master.do?sys_id=' + current.sys_id;
} else {
    producer.portal_redirect = "/cca?id=cca_thankyou";
}

 

Jim Coyne
Kilo Patron

Simplest would be to use the gs.hasRole() method:

 

if (gs.hasRole("contract_admin"))){  //hasRole will return true if the current user has the specified Role
    producer.portal_redirect = 'u_contract_master.do?sys_id=' + current.sys_id;
} else {
    producer.portal_redirect = "/cca?id=cca_thankyou";
}

 

You don't actually specify where the userRoles variable is from or gets its data.