Salesforce: How to create multiple records in a flow using a Record Collection Loop

tl:dr: Creating multiple records within a loop runs into governor limits and is generally inefficient. Instead, we need to create two variables, one to ‘create’ the record and one to collect these records. Then we can insert all of the records at the end of the flow, outside the loop.

I don’t get to play with complex flows very often. Most of the things that I want to do with loops are much faster and cleaner in Apex, so those items get handed over to the devs pretty quickly once we’ve identified the need.

But every so often, we need to build something the client can evolve over time. And that’s the use case for flows – complex automations that can still be amended without the need to read and understand lines and lines of code.

Recently, I’ve had a few use cases where I needed to create a set of records based off another set of records. Once to calculate planned invoicing per month in a consistent table (so I generated a record per month for the duration of the project and then populated them), other times using the outputs of a screen flow to generate the records necessary to drive billing rates.

Because these collections and loops can get big, efficiency was key, so generating records within the loop was not going to cut it. And I had an oddly hideous time getting to the bottom of how to create a ‘staging table’ within the flow. So I’ve documented it here for prosperity – so that in another six months when I need to use this again, I will have instructions that work for my brain. Maybe they will be useful for other people too.

Two Collections For Staging

Here’s where my brain was struggling to translate: I knew I needed to get the information, process it, set the values for the fields on the new records, and then insert them at the end.

What I couldn’t find documented anywhere was this:

You need two record variables to handle this – one to to set the values for each field, and then another record collection variable which is populated by the values from the first.

It seems so simple now I’ve done it a dozen times, but I could not find this written down or screen-capped anywhere.

Variables:

  1. Records to Create – this is a variable of the type ‘record’ that only allows a single record. You use it in your initial assignment in the loop to specify the fields for a single record.
  2. Records to Insert – this is a variable of the type ‘record’ that is also a collection (it allows multiple records). You assign your ‘Records to Create’ resource to this collection at the end of your loop, and then use the collection to upsert records at the end

Super simple – but somehow not written down anywhere I could find!

Step by Step

Let’s work it through so we have pictures as well as words in the future:

Create Two Record Variables

As discussed, we need two variables to manage this process. Our first variable is a single record variable (do not check the Allow multiple values (collection) box for this one.

I tend to call this ‘{Records} to Create because it helps remind me about the two step process:

Now we need a second variable which is a collection. We populate this at the end of the assignment step, taking the entire resource of the Records to Create variable in that step:

Use an Assignment to populate Records to Create

Within our loop, we need to use the Assignment feature to populate our variables. We first use the ‘Records to Create’ variable to set the fields for each record in turn, using the current item from the loop.

Remember that we choose the variable first, and then the specific field that we want to set:

The Records to Create variable will allow us to populate any valid field on the record we are creating:

Assign Records to Create to Records to Insert

You can do this at the end of the first assignment, but I’ve added a second element to make things clearer.

Once we have populated our Records to Create variable, we want to add it to the collection of Records to Insert:

This means our loop can set the values for all the record on the field and then send it to a holding pen so we can create the records all in one go at the end.

Insert or Update Records at the End

Outside the loop (after the last record) we can use the Update Records action to create the records based on our collection Records to Insert.

Choose the third option – Use the IDs and all field values from a record collection:

The Full Flow

Here’s how it looks on the screen canvas:

Why This Approach Matters

It is possible using the flow elements to create each record within the loop – and if you only ever insert one or two records, you might get away with this approach. However, you will quickly run into governor limits due to the amount of requests you are making.

By holding the values in the variable and inserting at the end, you vastly reduce the number of queries you are making, keeping the process clean and efficient at scale.

Comments

Leave a Reply