Find users having role snc-internal or no role in Business rule

SNOW44
Mega Guru

Hello Everyone,

I have an interesting requirement where end users are  not allowed to rename/delete attachment in portal. End user having both scenario which they have snc_internal or no role to user. And for user who don't have any role have solution like !gs.hasRole() == true.

My question is how to we find the user who have exactly snc_internal role?

can some one help me on this. @Ankur Bawiskar 

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @SNOW44 ,
I trust you are doing great.
PLease find the below solution for the same.

// Check if the current user has the snc_internal role
function hasSNCInternalRole() {
    var hasRole = false;
    var user = gs.getUser();
    var roles = user.getRoles();
    
    for (var i = 0; i < roles.size(); i++) {
        if (roles.get(i) == 'snc_internal' && roles.size() == 1) {
            hasRole = true;
            break;
        }
    }
    
    return hasRole;
}

// Example usage
if (hasSNCInternalRole()) {
    // User has only the snc_internal role
    gs.info("User has exactly the snc_internal role.");
} else {
    // User does not have exactly the snc_internal role
    gs.info("User does not have exactly the snc_internal role.");
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

8 REPLIES 8

Sumanth16
Kilo Patron

Hi @SNOW44 ,

 

Query User has a role table in Business Rule and checks user has no role as well as snc_internal by pushing into an array and check the array length and index of snc_internal.

 

 

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', gs.getUserID());
gr.query();
answer = gr.hasNext()

 

  

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

Thanks Sumanth, will check and comeback!

Amit Gujarathi
Giga Sage
Giga Sage

HI @SNOW44 ,
I trust you are doing great.
PLease find the below solution for the same.

// Check if the current user has the snc_internal role
function hasSNCInternalRole() {
    var hasRole = false;
    var user = gs.getUser();
    var roles = user.getRoles();
    
    for (var i = 0; i < roles.size(); i++) {
        if (roles.get(i) == 'snc_internal' && roles.size() == 1) {
            hasRole = true;
            break;
        }
    }
    
    return hasRole;
}

// Example usage
if (hasSNCInternalRole()) {
    // User has only the snc_internal role
    gs.info("User has exactly the snc_internal role.");
} else {
    // User does not have exactly the snc_internal role
    gs.info("User does not have exactly the snc_internal role.");
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Ankur Bawiskar
Tera Patron
Tera Patron

@SNOW44 

what did you start with and where are you stuck?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader