what activities can be used to iterate through an array? for each row while for each flow decision
When you're building a Salesforce Period, at that place are often times where you've got a collection of items and yous want to perform a specific activeness or check an individual particular. For example, let'due south say y'all wanted to create a number of Tasks related to a record when Users made selections on a Multi-Picklist. Rather than layering the Flow with multiple Conclusion elements (i.e., Did they select Option A, did they select Option B and so on) – this is where Loops would come up in to simplify the Flow processing.
If you're looking for a way to perform a specific action more than once, Loops are going to be your best friend. In this commodity, we'll swoop into what Loops are, how to use them, and some best practices to keep in mind.
What is a Loop in Salesforce Catamenia?
A Loop is a Salesforce Menstruum chemical element that is used to iterate through a number of items in a collection variable.
There are iii main components of a Loop:
- Collection Variable: This is the Collection y'all want to loop through – the Collection contains multiple Variables, each of which you lot want to either assess or activeness.
- Loop Variable: This will exist the temporary property identify of a single Variable from within the Collection every bit it is being processed. This can either be created manually or tin can exist created automatically when setting upward the Loop.
- Direction: This allows you to cull whether you want to Loop from first to last, or last to first item in a Collection. In a lot of basic scenarios, this isn't so critical.
How to Create and Employ a Loop
Let'due south use this scenario: An Business relationship has an Active__c Checkbox field, as does the Contact object. Your managing director has asked you to create a Flow that marks all child Contact records as Active or Inactive, based on the value of the Account'southward field. There are multiple means to do this – one of which is to use a Loop to iterate through all the Contact records and utilise an Assignment to set the new value.
Note: In that location are definitely better ways to execute this example, simply this example provides a very simple way to demonstrate the Loop feature, and so we're going with it!
If you're post-obit forth at home, all you demand to practice is ensure there'south an Active__c Checkbox field on both the Account and Contact.
The first step is to create a new Tape-Triggered Flow that runs After Update. This volition be triggered whenever an Account record's Active field is changed.
The side by side thing you lot need to practise is collect all the relevant Contact records into a Drove so that you tin process them in a Loop. After the Start element, create a Become Records element as below:
Ensure that you've selected All Records under the How Many Records to Store header. This ensures that a Collection, rather than a single tape variable, is captured.
At present that you take your Drove, it's time to loop through the records and change the Active field, co-ordinate to the Account's new value. To do this, create a Loop afterwards the Get Records element. Use the Collection that was created in the Go Records element.
The next step is to Assign the new value, using an Consignment. (All-time practice tip incoming!) Never perform a DML Argument within a Loop! Nosotros'll talk more about this afterward.
You're going to perform a 'double assign' – this is a nickname I've given to the method of assigning a Variable and and then assigning it to a new Drove.
The first Assignment will be used to prepare the new Active value on the Contact Variable. Create your Consignment Variable within the Loop every bit follows:
The second Assignment volition be used to put the Contact into a new Collection that you'll later apply to update all the Contact records at in one case. Firstly, you'll need to create a new Contact Collection Variable as follows:
You at present need to assign the Contact record to the new Collection variable. This is so that you have a single Drove variable to update subsequently the Loop has closed. This Assignment needs to exist created within the Flow, only after the outset Assignment. Create your consignment as follows:
Finally, you need to create your Update at the cease of your Flow. This will commit the changes you've assigned and update the Contact records. Create your Update element equally follows:
That's it! Your Menstruation is complete. All you need to do now is examination information technology (we won't go into that in this article). Your completed Flow should look something like this:
Best Practices When Using Loops in Salesforce Flow
While creating the above Flow, nosotros've already discussed some best practices that need to be taken into account while using Loops in Salesforce Flows. Permit's reiterate and get into farther item:
Never perform a DML statement inside of a Loop.
Avoid using the pink Data elements within a Loop. There's no amend manner to avert hit a governor limit than designing to avoid them!
In the Flow above, you're only pushing a unmarried DML statement – the Update Contacts at the stop of your Flow. If you lot'd use the Update Contact element inside the Loop instead of using the double assign tactic, there's no way to predict how many times it'll be used. Yous may accept an Account with over a thousand contacts, which ways the Flow will attempt to use the Update call a one thousand times – this volition fail. If you assign a thousand records and utilise the Update call once (as you've done above), you won't encounter the same result.
Assign to a new Collection rather than reusing the original one.
Reusing a variable is never a skillful idea, especially when working with Loops. It'southward ameliorate to create a new Collection Variable and populate it with your updated records, for employ afterward in the Flow (in the example above, updateContact was your new Collection Variable).
Summary and Further Learning
In the above example, yous learned how to use Loops in a Menstruum and also some key best practices to keep in mind, when using Loops. If you'd like to learn more about using Loops and using Flows in full general, at that place'due south a few options I'd recommend.
Firstly, y'all have Trailhead. Trailhead is Salesforce'southward complimentary training platform that has multiple Flow-based modules, all of which yous can study at your own stride.
One more source that I used quite a bit when learning most Flows back in my early days was Rakesh Gupta'due south AutomationChampion.com. There are a bunch of fantastic examples of Flows that utilize Loops (like this one). They're a little bit more than circuitous than Trailhead, but offer a great next stride for those who are looking to further their Flow teaching.
Source: https://www.salesforceben.com/salesforce-flow-loops-best-practices-and-examples/
0 Response to "what activities can be used to iterate through an array? for each row while for each flow decision"
Post a Comment