Remove entries from a list

PiotrekL
Tera Expert

Hi

 

I have a field where many groups can be stored, a list of groups. I need to remove only specific group from this list. In below case Agile Expert. I wrote below code but it is not working OK. Some people have correct group removed but for some I have many duplicates of other groups which were there but now they are multiplied i.e. 10 times, for some I see new groups added to this filed which were not there before. Pls advise what might be wrong in the code.

 

Reaards

 

var removedGroups = [];
var grUser = new GlideRecord('sys_user');
grUser.addQuery('u_removed_groups_inactivityLIKEf6366c100fd28200607500ace1050ezz');
grUser.query();
while(grUser.next()){

removedGroups.push(grUser.u_removed_groups_inactivity);
removedGroups = removedGroups.toString().split(',');
removeAgileExpert(removedGroups);


}

function removeAgileExpert(removedGroups){

                for (var i = 0; i < removedGroups.length; i++){
                    if (removedGroups[i] === 'f6366c100fd28200607500ace1050ezz') {
                                var spliced = removedGroups.splice(i, 1);
                                grUser.u_removed_groups_inactivity = removedGroups.toString();
                                grUser.setWorkflow(false)
                                grUser.update();
                                
                            }
                        }
                    }

          

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@PiotrekL Please use the following code and see if it works.

var removedGroups = [];
var grUser = new GlideRecord('sys_user');
grUser.addQuery('u_removed_groups_inactivityLIKEf6366c100fd28200607500ace1050ezz');
grUser.query();
while(grUser.next()){

removedGroups.push(grUser.u_removed_groups_inactivity.toString());
removedGroups = removedGroups.toString().split(',');
removeAgileExpert(removedGroups, grUser);

}

function removeAgileExpert(removedGroups, grUser){

                for (var i = 0; i < removedGroups.length; i++){
                    if (removedGroups[i] === 'f6366c100fd28200607500ace1050ezz') {
                                var spliced = removedGroups.splice(i, 1);
                                grUser.u_removed_groups_inactivity = removedGroups.toString();
                                grUser.setWorkflow(false)
                                grUser.update();
                                
                            }
                        }
                    }

Hope this helps.

View solution in original post

2 REPLIES 2

Tony Chatfield1
Kilo Patron

Hi, as removedGroups[] is declared outside of your while loop, the variable is effectively updated at each loop with a new element added based on the next grUser record.
You should be able to resolve this by moving the instantiation of removedGroups[] into your while loop (as the first entry) but I see no reason for this array and while you can split grUser.u_removed_groups_inactivity and iterate through it as an array, you could also simply use string methods to deliver the same solution. Maybe not technically the best code solution but this may be a clearer/easier to understand result and as something that is not run every few seconds the impact would be minimal.
Untested but maybe something like this.

var mySysId = 'f6366c100fd28200607500ace1050ezz';
var myQuery = 'u_removed_groups_inactivityLIKE' + mySysId;

var grUser = new GlideRecord('sys_user');
grUser.addEncodedQuery(myQuery);
grUser.query();
while(grUser.next()) {

var stringCheck = grUser.u_removed_groups_inactivity.toString();

if(stringCheck.indexOf(mySysId) > -1) {
stringCheck.replaceAll(mySysId, '');

if(stringCheck.indexOf(',,') > -1) {
stringCheck.replaceAll(',,', ',');
}
grUser.u_removed_groups_inactivity = stringCheck;
grUser.setWorkflow(false);
grUser.update();
}
}

 

Sandeep Rajput
Tera Patron
Tera Patron

@PiotrekL Please use the following code and see if it works.

var removedGroups = [];
var grUser = new GlideRecord('sys_user');
grUser.addQuery('u_removed_groups_inactivityLIKEf6366c100fd28200607500ace1050ezz');
grUser.query();
while(grUser.next()){

removedGroups.push(grUser.u_removed_groups_inactivity.toString());
removedGroups = removedGroups.toString().split(',');
removeAgileExpert(removedGroups, grUser);

}

function removeAgileExpert(removedGroups, grUser){

                for (var i = 0; i < removedGroups.length; i++){
                    if (removedGroups[i] === 'f6366c100fd28200607500ace1050ezz') {
                                var spliced = removedGroups.splice(i, 1);
                                grUser.u_removed_groups_inactivity = removedGroups.toString();
                                grUser.setWorkflow(false)
                                grUser.update();
                                
                            }
                        }
                    }

Hope this helps.