Most Efficient Way to Replace an Item in a Collection


Userlevel 6
Badge +15

Hello!

SP 2013 Nintex WF

What is the most efficient way (least actions) to REPLACE a value in a collection.

I take something out of the collection, I modify it, I want to put it back in at the same index. I don't want the number of collection items to increase. 

Or does "Add" at a specific Index overwrite if the index already exists?

Thanks!


4 replies

Userlevel 6
Badge +15

I feel like this question will be easily answered by: Andrew Glasser‌ | Marian Hatala‌ | Cassy Freeman

Userlevel 5
Badge +14

If I knew that I was going to work on the value @ whatever index, I would just complete whatever work I had intended to do with it in the loop that was there. However, if I had steps to take in between a Collection in its original state and some altered state, I would just push my altered values into their very own Collection. 

Because a For Each loop starts at the lowest index, the order of the altered / updated / worked values get pushed into the new Collection would be the same as the existing one. Because of this all you would need is a blank Collection workflow variable, and a single Collection action that added the target value to the said variable. No Index tracking is needed! 


If you use Add @ Index in an attempt to reuse the existing Collection, I'm almost sure that it will push any values from that index up (if a value exists at that index, and that index is valid) up by one index number, and pushes whatever info you're assigning to the targeted index into that position. 

[A,B,C] => Add "D" @ index (1) => [A,D,B,C] 

Edit: I guess I should answer the question of "but what if I do wanna use the same Collection!?" 

If you know which index you have taken a value from (because you are specifying it in some way manually or using a workflow variable in a loop to store the current index) then by adding a new value back into that index location should move everything up by +1 as shown above. 

All you would do then is use another Collection Action to Remove at that same index +1

[A,B,C]: (3) indexes. Index 0 = A, Index 1 = B, Index 2 = C. 

Add Value "2" at Index 1

[A,2,B,C]

Remove at Index 2


[A, 2, C] 

Userlevel 6
Badge +15

Yeah, that's how I've been doing it (more or less) .. I remove @ Index 1, add at Index 1, and seems to work out fine. Just feels like too many steps :/  I've been hopeful there was a "replace" i just wasn't seeing haha.

Userlevel 5
Badge +14

That's why I advocate just pushing the altered value directly into another collection that will be worked somewhere down the line. It can also just make it more clear, if you're like me, and might not come back to a particularly awful / complicated workflow for a few months. 

Mostly just down to preference, as most things are in the world of programming! 




Reply