Consulting Flow General Giving Back

Things People Ask Me: Cloning in Reverse Order

Helping a friend with a cool Flow requirement to clone some records in reverse order.

I looove helping people with things! I like it because I’m just a nice person, but it also helps keep my mind fresh I think. You know how it goes… you start doing a project and that’s all you do all the time and you feel like your other skills get a little rusty because you aren’t applying them. Helping people with random requests totally makes me feel like I can still use the other half of my brain, and it makes me feel good! It’s a win-win for everyone!

So today, I was talking with a friend and she had a good one for me. She has a set of records where each one is essentially a step you take to complete a task. Think about the steps it would take to build a chair, maybe. Step 1: tighten this screw; Step 2: add this piece, etc. She needed to be able to clone all of these steps in REVERSE ORDER so that someone could follow those steps in reverse. Think: disassemble the chair. She was trying to accomplish this in a flow, which of course is the best option, but couldn’t quite get it. So I said leave this with me, I will figure it out and come back. This one was especially fun because its not something that anyone has asked me for before. Yay new things! So, here’s how I solved it:

Create a Dummy Object

I don’t have access to the org that my friend is working in, of course, so I created a really simple dummy object with just the stuff that I needed. Here’s what I ended up with:

The important fields here that I end up referencing elsewhere are the Sequence Number, the Type, and the Random Field 😅. After I created the Object, I created some dummy records that I could use in my Flow.

Let’s review our requirements again before we get into the Flow:

  1. Clone all the records that have a Type of “Type 1”.
  2. Each cloned record should have a reversed sequence number. So, what was #1 should become #5 and so on.
  3. Each cloned record should have a Type of “Type 2” instead of “Type 1”.
  4. Each cloned record should have all the same values in all other fields except for Random Field should be blank.

The Flow

She is using a Screen Flow for her implementation, so I did the same just to demonstrate it was possible with all the same functionality. I added a quick screen that just asks if you’re sure you really want to clone these records.

And then since I asked if they were sure, I added a Decision element to see what they said. Only fair, right?

And now we get to the meat of the solution! We’re only working on the Yes path of our Decision element, even though it doesn’t really matter for our fake data. First we’re going to do a Get Records to find our Type 1 records that we want to clone.

The important thing here is that we want to make sure we are storing all records so that Flow creates a collection for us. If you notice, I’m not sorting these and I’m also automatically saving all fields.

Next I’m going to add a Collection Sort. They introduced this a couple releases ago and I haven’t used it yet, so I was excited to add it to the canvas! I’m sorting my new collection variable by the Sequence Number in descending order. This means my Sequence Number 5 is going to be first in the list, and Sequence Number 1 will be last.

Did I really need this step? Probably not? I could have just sorted the results in the Get Records probably, but I wanted to use this new element!

Now I’m going to add a Loop. We need to loop through each record in our collection and do some stuff. I’m going to iterate over the collection from first to last, which if you remember from the previous step, is highest to lowest.

Now comes the fun! Inside my Loop, I’m adding an Assignment element, and this is what is really doing all the work for us.

The first Assignment we’ll do, is to assign the value of “Type 2” to the Type field of our current record. This will update the Type of our new record that we’ll create later.

Next, I’m introducing a new variable called loopCounter and I’m going to add 1 to that each time the Loop runs. Essentially, I’m counting how many times the Loop is running.

And then I’m going to assign the value of my loopCounter to the Sequence Number field on my current record. Since I’m iterating through these records backwards, the count of the loop iteration will always be my new Sequence Number. Boom!

Next I’m assigning an Empty String value to the current record’s Random Field field. If you remember, we automatically saved all fields in our Get Records element, because I wanted to copy all the other values. With the exception of this one. But instead of selecting other field values in the Get Records, I’m just going to clear the value here.

And finally, I’m going to assign my updated record to a new collection variable called cloneCollection. This variable will store all my records that I want to create later on. Do I really need to move the records to a different collection? Probably not, since I already assigned the new values. But it makes me feel more organized to move them to a different bucket.

So this is what that Assignment element looks like in the end:

Now I’m done with the loop and all that’s left to do is to create my new records! In my Create Records element, I want to be sure I’m selecting to create multiple records, and I’m going to create them from my cloneCollection collection.

And that’s it! This is what the final Flow looks like:


To test this, I just added a Flow component to the Lightning Record Page of my object.

After clicking Yes and Next, I can navigate back to my list view and see my results.

You’ll see that what started as a Type 1, Sequence Number 1 is now a Type 2, Sequence Number 5 and the Random Field has been cleared.

And that’s it! Requirement solved! 😁

0 comments on “Things People Ask Me: Cloning in Reverse Order

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: