Remove item from a list collector

mballinger
Mega Guru

Hello,

How can I remove 2 items from a list collector? Every record will have multiple items in the list collector.

This is what I have and it is not working:

var gr = new GlideRecord("incident");
gr.addEncodedQuery("u_userLIKEd5f94d1487b9b0d0d2c7c9d6cebb355a^ORu_userLIKE3c23dcaa871a34d4d2c7c9d6cebb353d");
gr.setLimit(1);
gr.query();
while (gr.next()) {
    var users = gr.u_user.toString();	
    var usersArr = users.split(",");
    gs.print("List End: " + usersArr);

    if (usersArr.includes("d5f94d1487b9b0d0d2c7c9d6cebb355a")) {
        usersArr.splice("d5f94d1487b9b0d0d2c7c9d6cebb355a");
    }

    if (usersArr.includes("3c23dcaa871a34d4d2c7c9d6cebb353d")) {
        usersArr.splice("3c23dcaa871a34d4d2c7c9d6cebb353d");
    }

    gs.print("List End: " + usersArr);
}

Thanks!

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

Can you give more information as to how it is not working?

Are you seeing errors?

With that said, you can try using:

var gr = new GlideRecord("incident");
gr.addEncodedQuery("u_userLIKEd5f94d1487b9b0d0d2c7c9d6cebb355a^ORu_userLIKE3c23dcaa871a34d4d2c7c9d6cebb353d");
gr.setLimit(1); //if you're limiting it to one, why use while loop? unless you're just testing with one...
gr.query();
while (gr.next()) {
    var users = gr.getValue('u_user');	
    var usersArr = users.split(",");
    gs.print("List End: " + usersArr);
var index1 = usersArr.indexOf("d5f94d1487b9b0d0d2c7c9d6cebb355a");
var index2 = usersArr.indexOf("3c23dcaa871a34d4d2c7c9d6cebb353d");

    if (index1 > -1) {
        usersArr.splice(index1, 1);
    }

    if (index2 > -1) {
        usersArr.splice(index2, 1);
    }

    gs.print("List End: " + usersArr);
}

There's a few ways to do this, but the above would be one.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

11 REPLIES 11

Allen Andreas
Administrator
Administrator

Hi,

Can you give more information as to how it is not working?

Are you seeing errors?

With that said, you can try using:

var gr = new GlideRecord("incident");
gr.addEncodedQuery("u_userLIKEd5f94d1487b9b0d0d2c7c9d6cebb355a^ORu_userLIKE3c23dcaa871a34d4d2c7c9d6cebb353d");
gr.setLimit(1); //if you're limiting it to one, why use while loop? unless you're just testing with one...
gr.query();
while (gr.next()) {
    var users = gr.getValue('u_user');	
    var usersArr = users.split(",");
    gs.print("List End: " + usersArr);
var index1 = usersArr.indexOf("d5f94d1487b9b0d0d2c7c9d6cebb355a");
var index2 = usersArr.indexOf("3c23dcaa871a34d4d2c7c9d6cebb353d");

    if (index1 > -1) {
        usersArr.splice(index1, 1);
    }

    if (index2 > -1) {
        usersArr.splice(index2, 1);
    }

    gs.print("List End: " + usersArr);
}

There's a few ways to do this, but the above would be one.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

@Allen Andreas - Thanks for your response. Sorry, should've been a little more descriptive with my explanation. The code I have to remove certain items from the array is not working. When I executed my code, the userArr printed the same result without remove the specified sys_ids if it was contained in the list collector. 

I tried to run the script you provided me, and the same results. When I output userArr in the beginning of the code and in the end of the code it is outputting the same list.

Also, the setLimit(1) is just for testing. Didn't want to modify all records until I had it working

***EDIT***

How would I update the record?

gr.u_user = usersArr;
gr.update();

 

Hi,

Please ensure you're using the latest edit of mine as I posted then made an adjustment.

Yes, to update the record in the end, you'd do:

gr.setValue('u_user', usersArr);
gr.update();

I tested this prior to posting such as:

var array = ["d5f94d1487b9b0d0d2c7c9d6cebb355a","3c23dcaa871a34d4d2c7c9d6cebb353d"];
var index1 = array.indexOf("d5f94d1487b9b0d0d2c7c9d6cebb355a");
gs.info(index1);
var index2 = array.indexOf("3c23dcaa871a34d4d2c7c9d6cebb353d");
gs.info(index2);

if (index1 > -1) {
array.splice(index1, 1);
}

// if (index2 > -1) {
// array.splice(index2, 1);
// }
gs.info("List End: " + array);

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hey Allen - I used the latest edit of yours. The userArr is still displaying same elements. This is being handled in a fix script. Not sure if that matters