How To

Let’s Get With the Flow – Referencing Newly Created Records in a Flow

04_bails_Loops

This blog post is a continuation of the loop basics, focusing on how to reference newly created record Ids later in the flow.  The original post showed you how to update the record in the loop. The second blog post focused on how to update a record related to the record in your loop.

Here are a few lessons learned from implementing this use case:

  • Learn how to create a loop, set values via assignments, add another record to a collection and handle DML (database actions) outside of the loop.
  • Provide descriptions, where provided, in Salesforce. This may be tedious step, I know, but your future self will thank you when you are trying to remember what you configured or assist other/future admins when troubleshooting or enhancing what was built. This includes variables, the purpose of a flow, what each flow element does, etc.

What’s a loop? Think of it as an assembly line. Every item going down the assembly line has the same actions taken on it. In the Salesforce world, if you have a group of records that you need to perform the same actions on for every record in this grouping would go through a loop. Each record, when it is its turn to go down the assembly line will be placed into a record variable, let’s call it a loop variable. After it’s done with its turn, then that record is removed from loop variable and the next record in the collection is placed into the loop variable. This continues to happen until we are done with the last record in the collection and therefore, ends the loop.

A collection variable is a list of records you wish to take action on. A loop uses a loop variable to store the values for the current record in the collection. When the loop completes taking action on the one loop record, Salesforce then places the next record into the loop variable. If you want any database action to be taken on any record or record related to the record in the loop, you place it into a collection, to be handled outside the loop.

Since it is bad practice to perform a DML action (any database action) within a loop to avoid hitting governor limits, the best practice is to add records to a collection that will be actioned upon once the loop is completed.

But what if you have a group of records that is created in a loop in your flow and now you want to be able to reference those newly created Ids later in the same flow? How do you know what those Ids are since they aren’t stored in a variable?

Ah, but they are stored in a variable. You just don’t know that they are. In fact, the newly created ids are stored in the same collection variable they were created from.

Business Use Case:  Addison Dogster is the system administrator at Universal Containers. Mary Markle is the Director of Operations. She would like to create a group of contacts as part of automation and then in that same process, create a task for each newly created contact.

Solution: This blog post is not intended to cover the steps to build the entire flow solution that references the newly created ids in the flow, but only the portion of the flow that covers how you reference them.

The outcome looks like this:

MultipleLoops.JPG

This flow does the following: (1) gets accounts that meet a certain criteria, (2) put each record in a loop record variable, (3) assign contact values  for each loop record, (4) add the contact record to a collection of contacts to be created. (4) After the loop is completed, the contact records in the collection variable are created. (5) Then, for all the newly created contacts, place the contact record from the collection into a loop variable, (6) for each loop record, assign task values and then assign the task record to a collection. (7) After the loop is completed, the task records in the collection variable are created.

Highlighted Steps: 

1. In the flow, we would configure the following flow resources.

A. We need to create a record collection variable which holds the contacts to be created from the first loop.

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

This is how that flow resource would be configured.

    • Resource Type: Variable
    • API Name: ContactCollectionToBeCreated
    • Data Type: Record
    • Object: Contact
    • Allow multiple values (Collection): checked

ContactCollectionToBeCreated.JPG

B. There is a second record collection variable that holds the task records to be created from the second loop.

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

This is how that flow resource would be configured.

    • Resource Type: Variable
    • API Name: TaskRecordsToBeCreatedCollection
    • Data Type: Record
    • Object: Task
    • Allow multiple values (Collection): checked

TaskRecordsToBeCreatedCollection.JPG

2. Next, we will configure the second loop (as shown in the screenshot below) which requires the ids of the newly created contacts from the first collection.

MultipleLoopsHighlighted

A. We will use Loop flow element to start the second loop which will take each newly created contact and create a task associated to it.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure to match the below:

    • Collection Variable: {!ContactCollectionToBeCreated} [Note: This is how to access the newly created Ids. Just reference that same collection to unlock them.]
    • Direction: First item to last item
    • [Note: The Loop Variable is {!Current Item from Loop_Loop1}]

MultipleLoops-Loop1View image full screen

B. Next, we need an Assignment flow element to assign values to our task record – TaskRecord. TaskRecord is a record variable. We are setting the ownerId, Subject, Priority, Status and WhoId fields.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure to match the below:

    • {!TaskRecord.OwnerId} Equals {!TaskOwnerIdCustomLabel}
    • {!TaskRecord.Subject} Equals This is the subject
    • {!TaskRecord.Priority} Equals Normal
    • {!TaskRecord.Status} Equals Not Started
    • {!TaskRecordWhoId} Equals {!Loop1.Id}

MultipleLoops-AssignValuesToTaskAssignment.JPGView image full screen

C. Before we end the loop, we need another want Assignment flow element to assign the {!TaskRecord} record variable to a {!TaskRecordsToBeCreatedCollection} record collection variable to be created outside the loop. It is bad practice to create records within a loop (for fear of hitting governor limits), so we save DML actions for outside the loop. Assigning a record to a collection is done the opposite way than you would think. It’s not {!TaskRecord} Add {!TaskRecordsToBeCreatedCollection}. This can trip anyone up. Believe me, it’s done it to me many a time.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure to match the below:

    • {!TaskRecordsToBeCreatedCollection} Add {!TaskRecord}

MultipleLoops-AddTaskToCollectionAssignment.JPGView image full screen

Congrats! That’s it! You were able to successfully reference newly created ids created from a collection variable in your flow!

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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