Before BR not executing , trying to get users count whose department is finance

ravitejak
Tera Contributor
(function executeRule(current, previous /*null when async*/) {
var users = new GlideRecord('sys_user');
users.addQuery('department','finance');
users.query();
if(users.next()){
    // Add your code here
var count = users.getRowCount();
gs.info('18/09:user Count'+count);}
})(current, previous);
3 REPLIES 3

Eshwar Reddy
Kilo Sage

Hi @ravitejak 
While Loop iterates over multiple records returned by a query, executing the block of code for each record until there are no more records to process

Use the below code 

var users = new GlideRecord('sys_user');

users.addQuery('department.name', 'finance');

users.query();

var count = 0;

while (users.next())

{

count++;

}

gs.info(' User Count: ' + count);

Thanks 
Eshwar

 

Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution

Brad Bowman
Kilo Patron
Kilo Patron

The department field on the sys_user table is a reference, which means the value stored is a sys_id of the record you see on the Department table.  In a GlideRecord you can either replace 'finance' in your script with the sys_id of the Finance record, or use

users.addQuery('department.name', 'Finance');

vigneshsund
Tera Contributor

@ravitejak 

department is reference field and it will take sysID not the name. Try passing sysID you will get the count.

Exisiting:

users.addQuery('department','finance'); 

Change to

users.addQuery('department','sysID'); // get the 'Finance' sysID from cmn_department table