Skip to main content

Loop Through a Repeater

Learning Objectives

After completing this unit, you’ll be able to:

  • Loop through a repeater’s entries to add them to a single, unified collection.
  • Update and create records simultaneously from a single collection.
Note

This badge is one stop along the way to Flow Builder proficiency. From start to finish, the Build Flows with Flow Builder trail guides you through learning all about Flow Builder. Follow this recommended sequence of badges to build strong process automation skills and become a Flow Builder expert.

Let’s continue working with the Mass Create & Update Contacts flow from the previous unit.

Loop to Modify Records and Add Them to a Collection

Your flow is ready to capture data, but the flow needs to organize that data into a structure that it can use: a record collection variable. Transform elements can’t work with repeater data, so use a Loop element and an Assignment element to add all of the repeated components’ values to a record variable. You can then use that same Assignment element to add the contents of that record variable to a new record collection variable.

Record 1 is copied from repeater values into a record variable, and that record variable is moved to a collection variable.

When you reference a repeater’s values, you can select the repeater’s Added Items, Prepopulated Items, Deleted Items, or All Items.

  • Added Items contains only the repeater’s new records, and ignores its prepopulated records.
  • Prepopulated Items contains only the repeater’s prepopulated records, and ignores its new records.
  • Deleted Items contains only the records that the user deleted from the repeater.
  • All Items contains all of the repeater’s added, prepopulated, and deleted records.

Why are these options separated out? You might need to do different things with each group, such as using a Delete Records element to remove everything in Deleted Items from the org’s database. Make sure you use a separate loop to process each group of items.

In this flow, we don’t worry about deleted records, and at the end of the flow, we use a special method to create new records and update changed records all together. So here, we use the All Items option.

  1. Open the Mass Create & Update Contacts flow.
  2. After the Get Details element, add a Loop element.
    • For Label, enter LoopRepeater.
      The API Name should auto-populate as LoopRepeater.
    • For Collection Variable, select Get Details > ContactRepeater > All Items.
  3. Create a record variable to collect data from the components into a structure that the flow recognizes as a record.
    • For Resource Type, select Variable.
    • For API Name, enter CurrentContact.
    • For Data Type, select Record.
    • For Object, select Contact.
    • Click Done.
  4. Create a record collection variable to collect all of the repeater entries after each one has been structured as a record.
    • For Resource Type, select Variable.
    • For API Name, enter NewAndChangedContacts.
    • For Data Type, select Record.
    • Select Allow multiple values (collection).
    • For Object, select Contact.
    • Click Done.
  5. On the For Each path, add an Assignment element.
    • For Label, enter Add Repeater Entry to Collection.
      The API Name should auto-populate as Add_Repeater_Entry_to_Collection.
    • For Variable, select CurrentContact > Contact ID.
      Even though there’s no ID field in the repeater, you must include the record’s ID in the records that go in the collection. Without the record’s ID, the flow doesn’t know which record in the repeater corresponds with which existing contact record. As a result, the flow creates a new record for each record in the repeater, even if that record already exists. (Duplicate data alert!)
    • For Operator, select Equals.
    • For Value, select Current Item from Loop LoopRepeater > Id.
  6. Click Add Assignment to add another assignment line.
    • For Variable, select CurrentContact > Account ID.
    • For Operator, select Equals.
    • For Value, select varAccountID.
  7. Click Add Assignment to add another assignment line.
    • For Variable, select CurrentContact > First Name.
    • For Operator, select Equals.
    • For Value, select Current Item from Loop LoopRepeater > Name > First Name.
  8. Click Add Assignment to add another assignment line.
    • For Variable, select CurrentContact > Last Name.
    • For Operator, select Equals.
    • For Value, select Current Item from Loop LoopRepeater > Name > Last Name.
  9. Click Add Assignment to add another assignment line.
    • For Variable, select CurrentContact > Title.
    • For Operator, select Equals.
    • For Value, select Current Item from Loop LoopRepeater > Title.
  10. Click Add Assignment to add another assignment line.
    • For Variable, select CurrentContact > Email.
    • For Operator, select Equals.
    • For Value, select Current Item from Loop LoopRepeater > Email > Value.
  11. Click Add Assignment to add another assignment line.
    • For Variable, select CurrentContact > Business Phone.
    • For Operator, select Equals.
    • For Value, select Current Item from Loop LoopRepeater > Business Phone > Value.
  12. Click Add Assignment one more time to add a final assignment line.
    All of the lines before this line assembled a contact record in the CurrentContact record variable. This final line adds the contents of the CurrentContact record variable to the NewAndChangedContacts record collection variable.
    • For Variable, select NewAndChangedContacts.
    • For Operator, select Add.
    • For Value, select CurrentContact > Entire Resource.
  13. Save the flow.

The flow builder canvas and Assignment configuration panel corresponding to the preceding steps.

Upsert the New and Changed Contact Collection

Finally, add a Create Records element after the loop to push the new and changed records to the database.

You may be thinking, “Wait a second, this collection contains new and changed records. Why am I only adding a Create Records element and not an Update Records element?” In this final Create Records element, you use a special feature to perform an upsert. An upsert is what it sounds like: a combination of an update and an insert. It’s a data statement that updates the records that already exist and creates the records that don’t already exist, all at the same time. As long as your collection’s records have a unique identifier field that Salesforce can look at and say, “Yup, that’s definitely the same record”, you can perform an upsert with a Create Records element.

  1. On the After Last path, add a Create Records element.
    • For Label, enter Upsert Contacts.
      The API Name should auto-populate as Upsert_Contacts.
    • For How to set record field values, select From a Record Variable.
    • For How Many Records to Create, select Multiple.
    • For Record Collection, select NewAndChangedContacts.
  2. Enable the Update Existing Records toggle.
    This toggle enables the element’s upsert functionality.
    • Make sure “Field to use to identify existing records” is set to Record ID.
      Remember, in the Assignment element, you included the Contact ID in the CurrentContact record variable. Because the upserting collection has the contact record’s ID, select the Record ID option here.
  3. Save the flow.

The flow builder canvas corresponding to the preceding steps.

And that’s a complete flow! If you’d like, you can add this flow as a custom action button on an active account Lightning page, and test it out for yourself. Check out the Add a Flow as an Action on a Record unit in the Screen Flow Distribution badge for more information.

When you run the flow from an account, it displays any existing contacts for that account and allows you to enter details to create additional contacts.

The Mass Create & Update Contacts flow with two contacts prepopulated in the repeater.

Resources

Condividi il tuo feedback su Trailhead dalla Guida di Salesforce.

Conoscere la tua esperienza su Trailhead è importante per noi. Ora puoi accedere al modulo per l'invio di feedback in qualsiasi momento dal sito della Guida di Salesforce.

Scopri di più Continua a condividere il tuo feedback