TeHow to retrieve users from sys_user with the help of sysid of company and dept

Sachin G K1
Kilo Sage

Hi All,

 

How to retrieve the list of users from sys_user table with the help of sys_id of dept and company. Lets suppose sys id of company is 123 and sys id of dept is 456. i want to retrieve all the users from sys_user table having company sysid as 123 and dept sys id as 456.

 

 

 

 

Thanks in Advance

3 REPLIES 3

Manmohan K
Tera Sage

Hi @Sachin G K1 ,

 

Write below code  to retrieve the list of users from sys_user table 

// Provide the sys_id of the company and department
var companyId = '<sys_id of the company>';
var departmentId = '<sys_id of the department>';

var userGR = new GlideRecord('sys_user');

// Add a company and department query conditions to the GlideRecord
userGR.addQuery('company', companyId);
userGR.addQuery('department', departmentId);

userGR.query();

while (userGR.next()) {
  var userName = userGR.getValue('name');
  var userEmail = userGR.getValue('email');

  gs.info('User: ' + userName + ', Email: ' + userEmail);
}


 

Make sure to replace <sys_id of the company> and <sys_id of the department> with the actual sys_ids you want to use for filtering the users.  

Karthick9
Tera Contributor

Hi 

PLease try the below script

 

var a = new GlideRecord('sys_user');
a.addQuery('department','sys id of department');
a.addQuery('company','sys id of company');
a.query();
while(a.next()){
    gs.print(a.user_name);
}

Sai126
Tera Contributor
var userArray = []; //Array to hold users sys id's
var usrRec = new GlideRecord('sys_user');
usrRec.addActiveQuery(); //Add this, if you want to pull only active records
usrRec.addQuery('company', companySysID); //Put your company sys ID here
usrRec.addQuery('department', deptSysID); /Put your Department sys ID here
usrRec.query();
while(usrRec.next()){
    userArray.push(usrRec.getUniqueValue());
}
gs.log('List of User Sys Ids: ' + userArray.toString());