Show only the users whose manager is logged in user

Vijay485
Tera Contributor

Below script is not working when called in advanced reference qualifier 

 

var UserList = Class.create();
UserList.prototype = {
Requested: function() {

var managername = '';
var loggedInUserManager = '';
var a = new GlideRecord('sys_user');
a.addQuery('manager', gs.getUser().getRecord().getValue('manager'));
a.addEncodedQuery('active=true^u_worker_type=Contingent Worker');
a.query();
var users = [];
while (a.next()) {
users.push(a.getUniqueValue());
}
return 'sys_idIN' + users.toString();
},

type: 'UserList'
};

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, what exactly does 'not working' mean? What are the results of your debugging?
How is the function called? Does the function trigger\run? Can you run it via a background script?

1 issue is possibly that you are using getRecord() in an incorrect context.

getRecord() is not working in an Advanced User Criteria script - Support and Troubleshooting (servic...

How getRecord() method works - Support and Troubleshooting (servicenow.com)

swathisarang98
Giga Sage
Giga Sage

Hi @Vijay485 ,

 

You can try the below script which will give all the user which has same manager as logged in user,

 

Script include:

swathisarang98_0-1710882609791.png

 

var getUserManger = Class.create();
getUserManger.prototype = {
    initialize: function() {
    },

	getSameMangerUser: function(loggedInUser){
		
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',loggedInUser);
		gr.query();
		if(gr.next()){
			// gs.info('inside if logged in user ' + loggedInUser );
			var userManager = gr.manager;
			// gs.info('logged in user manager ' + userManager);
			var allUser = new GlideRecord('sys_user');
			allUser.addQuery('manager',userManager);
			allUser.addActiveQuery();
			allUser.query();
			var arr =[];
			while(allUser.next()){
				// gs.info('inside while ');
				arr.push(allUser.sys_id.toString());
			}
			
		}
		gs.info('array' + arr.toString() );
		return "sys_idIN" + arr.toString();
	},

    type: 'getUserManger'
};

 

 

reference qualifier:

swathisarang98_1-1710882648990.png

 

javascript:new getUserManger().getSameMangerUser(gs.getUserID())

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

@Vijay485 , Thanks for marking my answer as helpful. If it helped you in any way please accept the solution so that it will be beneficial to the future readers with the same query.

Deepak Shaerma
Kilo Sage

Hi @Vijay485 

Refine your script below. Please Check 

var UserList = Class.create();
UserList.prototype = {
    initialize: function() {
    },

    // Function to get the sys_ids of users that report to the logged-in user’s manager and are active contingent workers
    getRequestedUserSysIds: function() {
        var loggedInUserManagerSysId = gs.getUser().getManagerID(); // Get the logged-in user’s manager’s sys_id
        var userSysIds = [];
        
        if (loggedInUserManagerSysId) {
            var userGr = new GlideRecord('sys_user');
            userGr.addQuery('manager', loggedInUserManagerSysId);
            userGr.addQuery('active', true);
            userGr.addQuery('u_worker_type', 'Contingent Worker');
            userGr.query();
            
            while (userGr.next()) {
                userSysIds.push(userGr.getUniqueValue());
            }
        }
        
        // Prepare the encoded query string
        return userSysIds.length > 0 ? 'sys_idIN' + userSysIds.join(',') : '';
    },

    type: 'UserList'
};

// Example of how to use this script. Assume this is called from a Script Include, Business Rule, or Client Script as required.
var userListObj = new UserList();
var queryStr = userListObj.getRequestedUserSysIds();
gs.info(queryStr);

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma