The CreatorCon Call for Content is officially open! Get started here.

Query user role in advanced user criteria

Santiago Ruales
Tera Contributor

Hi experts, 

 

Please help. I need to validate, in an advanced user criterion (script), that the user has role knowledge and their manager has the title of IT Manager. I've tried this code, but it doesn't work. Any recommendations, please?

 

(function() {
    var user = new GlideRecord('sys_user');
    if (user.get(user_id)) {
var rol = false;
var gr = new GlideRecord("sys_user_has_role");
gr.addQuery("role", "xxxxda36c0a8016b888c7f06a1ce7e14"); //sys id knowledge role
gr.addQuery("user", user_id);
gr.query();
if (gr.next()) {
rol = true;
}
if (rol){
if (user.manager) { // Check if the user has a manager
var managerSysId = user.manager.title.toString();
if(managerSysId == 'IT Manager'){
answer=true;
}
else{
answer = false;
}
}
}
})();
9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@Santiago Ruales 

did you try debugging by adding that script in background script by hard-coding user sysId?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar, It works in the background, but not in the user criteria.

@Santiago Ruales 

Try doing this

1) add role in roles field in User Criteria

2) then use advanced script for manager title

3) ensure "Match All" is selected

var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", user_id);
gr.addQuery("manager.title", "IT Manager");
gr.query();
answer = gr.hasNext();

AnkurBawiskar_0-1758556269769.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

raviteja1600
Tera Guru

Hi @Santiago Ruales ,

 

As you mentioned you need to work this script in the User Criteria then you need to modify the script part as below

(function() {
    var userId = gs.getUserID(); //Get the logged in user
    var hasRole = false;

    // Check if the user has the specific role
    var roleGR = new GlideRecord("sys_user_has_role");
    roleGR.addQuery("role", "xxxxda36c0a8016b888c7f06a1ce7e14"); // Replace with actual role sys_id
    roleGR.addQuery("user", userId);
    roleGR.query();
    if (roleGR.next()) {
        hasRole = true;
    }

    if (hasRole) {
        var userGR = new GlideRecord("sys_user");
        if (userGR.get(userId) && userGR.manager) {
            var managerGR = new GlideRecord("sys_user");
            if (managerGR.get(userGR.manager.toString())) {
                answer = (managerGR.title == 'IT Manager');
                return;
            }
        }
    }

    answer = false;
})();

Avoid using sys_ids in the script directly, If this solution helped, please mark it as helpful and accept the answer.

 

Regards,

Raviteja

 

Dhana3
Kilo Sage

Hi @Santiago Ruales ,

 

In the user criteria add knowledge to the roles field, click on match all check box and in the script only check for manager title.
 
answer = function CheckManagerTitle();
 
function CheckManagerTitle(){
var curr_user = gs.getUserID();
var usr = new GlideRecord("sys_user");
usr.addQuery("sys_id", curr_user);
usr.query();
if (usr.next()){
if (usr.manager.title == "IT Manager"){
answer = true;
}
else{ answer = false;
}}