Script include to see logged in user is an manager

maneesh3
Tera Contributor

Hi,

I need to write an script include for an catalog item field: (field is an Reference field to user table) where in if User logged is an manager,  then only the manager employee's should be visible in the list of that reference field.

Please help me in this code

 

Thanks for the help!!!!

 

 

 

 

1 ACCEPTED SOLUTION

Karishma5
Tera Expert

hi maneesh,

Try with the following changes in your code:

 

function checkIfManager(){

var employees=[];

var currentUser = gs.getUser(); 

if(currentUser.isMemberOf(­'<Your group name>')){

var gr= new GlideRecord('sys_user');

gr.addActiveQuery();                                  // Filter to return active records.

gr.query();

while(gr.next()){

employees.push( gr.sys_id.toString());  

}

}

else{
var gr= new GlideRecord('sys_user');
gr.addQuery('manager', gs.getUserID());
gr.query();
while(gr.next()){
employees.push(gr.sys_id.toString());
}

}

return "sys_idIN"+employees;

}

 

 

Hope it helps.

View solution in original post

15 REPLIES 15

Karishma5
Tera Expert

hi maneesh,

Try with the following changes in your code:

 

function checkIfManager(){

var employees=[];

var currentUser = gs.getUser(); 

if(currentUser.isMemberOf(­'<Your group name>')){

var gr= new GlideRecord('sys_user');

gr.addActiveQuery();                                  // Filter to return active records.

gr.query();

while(gr.next()){

employees.push( gr.sys_id.toString());  

}

}

else{
var gr= new GlideRecord('sys_user');
gr.addQuery('manager', gs.getUserID());
gr.query();
while(gr.next()){
employees.push(gr.sys_id.toString());
}

}

return "sys_idIN"+employees;

}

 

 

Hope it helps.

maneesh3
Tera Contributor

Thanks a lot Karishma for your valuable help. This worked like charm. 

RDX
Tera Contributor

Hello,

 

I have the same issue where catalog item field: (field is an Reference field to user table) where in if User logged is an manager,  then only the manager employee's should be visible in the list of that reference field.

 

Above script is not working somehow. May be some update needed. 

 

Thanks in advance!

AishwaryaS
Tera Contributor

 I tried but not working as expected.

AishwaryaS1
Kilo Sage
Hi everyone, you can try below solution.
 
var UserListScriptInclude = Class.create();

UserListScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // Function to get a list of active users
    getActiveUsers: function() {
        var userList = [];
        var userRecord = new GlideRecord('sys_user');
        userRecord.addQuery('manager', gs.getUserID()); // Add conditions based on your requirements
        userRecord.query();

        while (userRecord.next()) {
            var userDetails = {
                userId: userRecord.getUniqueValue(),
                userName: userRecord.user_name.toString(),
                fullName: userRecord.getDisplayValue(),
                email: userRecord.email.toString(),
                // Add more fields as needed
            };

            userList.push(userDetails);
        }

        return userList;
    },

    // For testing purposes - you can call this function to get a list of active users
    testGetActiveUsers: function() {
        var activeUsers = this.getActiveUsers();
        gs.info(JSON.stringify(activeUsers, null, 4));
    },

    type: 'UserListScriptInclude'
});
Aishwarya Shelake