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

Hello,

Please share exactly what you're using as I've just edited my last reply with example code I was using in background script and it does work.

Verify your values, verify what I've given as well and you can "print" or gs.info those in background so you can see what index1 and index2 gives, etc.

-Allen


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

I'm getting into the right if block, but the array element isn't getting removed.

This is the code I used:

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, "d5f94d1487b9b0d0d2c7c9d6cebb355a");
		gs.print("first if" + usersArr);
    }

    if (index2 > -1) {
        usersArr.splice(index2, "3c23dcaa871a34d4d2c7c9d6cebb353d");
		gs.print("2nd if" + usersArr);
    }

	
    gs.print("List End: " + usersArr);
	gr.setValue('u_user', usersArr);
	gr.update();
}

Hi,

Please refer to my code as that's not what I have.

Please review once more.

As mentioned, I made an edit, please use the last version in my reply above.

To cut to the chase, it's in these lines:

usersArr.splice(index1, "d5f94d1487b9b0d0d2c7c9d6cebb355a");
usersArr.splice(index2, "3c23dcaa871a34d4d2c7c9d6cebb353d");

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


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

I guess I am not seeing what I am missing. I had to update "index" in the second if block to "index2",  I also added a few more print statements, as well as the setValue/update methods

***EDIT***

These are the results I am getting:

find_real_file.png

Hi,

You wouldn't need to guess and see what you're missing because I took your code and corrected it.

So what is in my reply above, way at the top, from about 45 minutes ago, has it all.

Please use that, paying specific attention to this section (if you're not going to copy what I did and are trying to eyeball it, lol...):

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

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

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


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