Code correction help

Jyoti Tripathi
Giga Guru

I have a requirement to assign the manager to user
1. if there are multiple manager profile with same employee number example ABC and ABC1 one is active and another is inactive, then user should be assign to active only
2. if there are multiple manager profile with same employee number example ABC and ABC1 and both are inactive, then user should be assign to inactive only

i was trying in background script

var mgr = 987651;
var tGrmanagers = [];
var fGrmanagers = [];

var grmanager = new GlideRecord('sys_user');
grmanager.addQuery('employee_number', mgr);
grmanager.query();

while (grmanager.next()) {
if (grmanager.active == true) {
tGrmanagers.push(grmanager.sys_id);
gs.info('tGrmanagers: ' + grmanager.sys_id);

}
else{

fGrmanagers.push(grmanager.sys_id);
gs.info('fGrmanagers: ' + grmanager.sys_id);
}
}

 

But issue is, loop is going in both if and else.. it is pushing both active and inactive. 

8 REPLIES 8

Harsh_Deep
Giga Sage
Giga Sage

Hello @Jyoti Tripathi 

 

Use this script-

var mgr = 987651;
var tGrmanagers = [];
var fGrmanagers = [];
var grmanager = new GlideRecord('sys_user');
grmanager.addQuery('employee_number', mgr);
grmanager.query();
while (grmanager.next()) {
if (grmanager.active == 'true') {
tGrmanagers.push(grmanager.getValue('sys_id'));
gs.info('tGrmanagers: ' + grmanager.getValue('sys_id'));
}
else{
fGrmanagers.push(grmanager.getValue('sys_id'));
gs.info('fGrmanagers: ' + grmanager.sys_id);
}
}

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

it is again picking both 

*** Script: fGrmanagers: 0c1bf0a81b588e50eddcfe25cc4bcb82
*** Script: fGrmanagers: 794ab0681b588e50eddcfe25cc4bcb92

Hi @Jyoti Tripathi ,

 

If you want to check only for the active account manager, remove the else part.

 

var mgr = ""; //Employee Number
var tGrmanagers = [];

var grmanager = new GlideRecord('sys_user');
grmanager.addEncodedQuery('employee_number=' + mgr);
grmanager.query();

while (grmanager.next()) {
    if (grmanager.active) {
        tGrmanagers.push(grmanager.manager.sys_id.toString());//To push active account manager sys id.
    }
}
gs.info(tGrmanagers);

 

 

 

-Dhanraj

Anand Kumar P
Giga Patron
Giga Patron

Hi @Jyoti Tripathi ,

Can you try with else if(grmanager.active==false) instead of else

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand