How to get a list of cost center names without having users

raj149
Giga Guru

Hello experts ,

 

my requirement is 

I want to create a report  that A list of cost centers without having users.

Here cost center(cmn_cost_center) is different table and user(sys_user) is different table.

 

how to achieve this by using client script..?

 

Thanks in advance.

 

Best regards,

Raj 

 

1 ACCEPTED SOLUTION

chrisperry
Giga Sage

Hi there,

 

You won't be able to do it with client script, you'll need a server script of some sort (business rule, script include, etc.).

 

The below script will get you a list of cost centers that do not have any users:

 

var resultArr = [];
var costCenterGr = new GlideRecord('cmn_cost_center');
costCenterGr.query();
while (costCenterGr.next()) {
    var userGr = new GlideRecord('sys_user');
    userGr.addEncodedQuery(gs.getMessage('cost_center={0}', costCenterGr.getUniqueValue()));
    userGr.setLimit(1);
    userGr.query()
    if (!userGr.hasNext()) {
        resultArr.push(costCenterGr.getUniqueValue());
    }
}
gs.info(gs.getMessage('Cost centers with no users: {0}', resultArr.join()));

 

View solution in original post

8 REPLIES 8

Bert_c1
Kilo Patron

Hi,

 

Seems there is a missing 'userGr.query();' line, before the 'if' statement. Otherwise the script seems to work.

Yes, good catch -- I had edited my reply a couple minutes after I posted it to include that userGr.query() line.

Hi Raj149,

 

chrisperry's report works for me, however in the Related List conditions, I didn't see "No" as a selection, but have "Less than 1".

hello @Bert_c1 

 

Select "None" option.