Loop Element

 

  • Loop Element

        • Using Loops the right way!

  • Loop Element


    The Loop element is used to cycle through items in a collection variable and perform some processing.

    The Collection Variable is one that contains one or more values or records.

    You can process the items inside the loop and then use assignment elements to update the values (in memory).

    Once you have completed this, the next item in the loop is processed and this continues until the last item is processed.

    The loop then terminates and the flow continues at the next element.

    Normally you would write these new values as part of an Update Record or Create Record after the loop is finished.

    It is important NOT to do the Update Records or Create Records (or delete) inside the loop.

    Doing so is bad practice as it can cause your flow to exceed Salesforce Governor limits.



    Sample Flow That Loops Through a Collection

    Here is a Flow that starts from the current Account record and checks if all related Contacts have an address.
    I
    f not, it will assign the Account address to these Contacts.

    After the loop finishes, the contacts without addresses will be updated.

    Step-by-step:

    1. Get the Account Record (we want the account address - only one record needed)
    2. Get all the Contacts related to our Account (store in a collection variable)
    3. Start our loop (input is the collection variable that holds all our related contacts)
    4. Check if the Contact Address is blank (Yes -> Step 5, No -> Step 3, next record)
    5. Assign the Account Address to the Contact Address (for the current record in the loop)
    6. Add the current record to our new Collection Record (contacts that we need to update) and
      -> return to Step 3, next record.
    7. When all records have been checked (our loop is complete), we update those records that we selected for an address update.




Create loops the right way!




Loop Element


The Loop element is used to cycle through items in a collection variable and perform some processing.

The Collection Variable contains one or more values or records.


It is important NOT to do the Update Records or Create Records (or delete) inside the loop.

Doing so is bad practice as it can cause your flow to exceed Salesforce Governor limits.



Flow That Loops Through a Collection

In this flow, you start from the current Account record and check if all related Contacts have an address.
I
f not, assign the Account address to these Contacts.

After the loop finishes, the contacts without addresses will be updated.

Here are the steps:

  1. Get the Account Record (we want the account address - only one record needed)
  2. Get all the Contacts related to our Account (store in a collection variable)
  3. Start our loop (input is the collection variable that holds all our related contacts)
  4. Check if the Contact Address is blank (Yes -> Step 5, No -> Step 3, next record)
  5. Assign the Account Address to the Contact Address (for the current record in the loop)
  6. Add the current record to our new Collection Record (contacts that we need to update) and
    -> return to Step 3, next record.
  7. When all records have been checked (our loop is complete), we update those records that we selected for an address update.





Create your new flow

Create a new Screen Flow.


Add a new variable recordId (type text) this will pass the account ID into the flow.



Add a new variable rc_ContactsToUpdate (a record collection of the contacts you need to update).




Step 1

Get one account record (the one that the ID has been passed through for):



Step 2

Get all the contact records for this account:



Step 3

Add a Loop to cycle through the records you have just retrieved:





Step 4


Add a Decision element and configure to check if Mailing City is blank.

(Note: You are only checking one field here, in reality you probably need to check more address fields)



Step 5

Assign new address values to the current record in the loop.



The values in here are:

Current Item from Loop Contacts_Loop > Mailing City  EQUALS Account from Get_Account > Shipping City

Current Item from Loop Contacts_Loop > Mailing Country  EQUALS Account from Get_Account > Shipping Country

Current Item from Loop Contacts_Loop > Mailing Zip/Postal Code  EQUALS Account from Get_Account > Zip/Postal Code

Current Item from Loop Contacts_Loop > Mailing State/Province  EQUALS Account from Get_Account > Shipping State/Province

Current Item from Loop Contacts_Loop > Mailing Street  EQUALS Account from Get_Account > Shipping Street


Step 6

Add this record to your record collection for later writing to the database.

Note the use of the Add operator here:


the value here is:

rc_ContactsToUpdate  Add  Currrent Item from Loop Contacts_Loop



Step 7

Now the loop is completed, update the contact records:



Save and activate your new flow.

Get the ID of an account that has several contacts and run your flow, passing the ID of the account as recordId.

Make sure several of these contacts have no value for Mailing City.




Were the contact addresses updated with the values from the Account record?

If yes ==> Celebrate smile

If no ==> go back and check what went wrong thoughtful

No comments: