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

Deepak Ingale1
Mega Sage
checkIfManager : function() { 

     var gr = new GlideRecord("sys_user"); 
     gr.addQuery("manager", gs.getUserID()); 
     gr.setLimit(1); 
     gr.query(); 
     
      return gr.hasNext(); // If user is manager for any of d employee in sys_user table 
}

Thanks Deepak for the script.

 

I need to call this script include in catalog item field, so now when user logged in is a manager then in this field only his/her employees should be visible. Can you please help me in the script that may need little change in your logic.

 

Thanks a lot for this help!!!

Karishma5
Tera Expert

hi  maneesh,

You can create a classless script include for this with the following code :

function checkIfManager(){

  var employees=[];
  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;
}

And in the catalog item field, you can right click on the field label and go to configure dictionary and in the reference specification section, select use reference qualifier field as Advanced and in the Reference qual field you can call your script include as follows and save the changes:

javascript :  checkIfManager()

 

Hope it helps.

Regards,

Karishma

 

Thanks a ton Karishma, Its working as expected. Also can you please help me in writing an if condition loop in the same script and return values for that field. Can you please give me sample.

 

Like I want to check in IF condition some group membership and for that user logged in I want to make different list of results.

 

 

Thanks again