I often see many different posts around people not quite sure how to use a Loop in Salesforce. Loops are often used to count a specific number of records. This could be for a few different reasons:
- Watching your Limits
- Remember, you can’t have more than 2,000 elements accessed in one transaction. You could use a counter to ensure that you stop before you’d hit that limit.
- Creating n Records
- If you want to create a specific number of records, like Tasks. I’ve seen this request come across on the Success Community and other places many times.
- Custom Roll-up Summary in Flow
- If you are’t able to use something like Andy’s DLRS or a standard Roll-up field, you can summarize an Amount or do a record count in Flow.
In this post, I’m going to go over the Watching your Limits scenario. If you want to have more information around a Custom Roll-up Summary in Flow, it’s actually one of my first posts (here)! I would also HIGHLY recommend watching Pete Fife’s Automation Hour presentation on Loops: http://automationhour.com/2017/02/pete-fife-deep-dive-into-loopsflow-21717/… he does a fantastic job covering the topic!
Let’s jump on into the details!
Most importantly, we’re going to need to create a variable to track our iterations inside our loop. So, let’s create that variable.
Now, we’ve got our variable that we will be able to track the number of times we go through our Loop. I’ve seen this inefficiently added it’s own Assignment inside a Loop. I’ll urge you to add this into on of your existing Assignments inside a Loop, because it won’t do any harm there, and saves you an element each Loop.
Every time we loop through a record, and we’re doing our Ownership reassignment, we’re adding 1 to the value of our LoopCounter. If we have 20 records, by the end of the transaction this LoopCounter variable would equal 20. If we had 10, it would equal 10.
In Flow, you have a limited number of elements that you can run through in each transaction. This means, you need to be extra careful when dealing with potentially higher data volumes. Ideally, you really shouldn’t be hitting close to that 2,000 elements limitation… however, it doesn’t hurt to be paranoid and put in a decision to double-check. So, that’s what we’re going to do. The value you use will vary based on how complicated your Loop is. You need to do some math to see the maximum number of records your Flow can take. I like to always go below that to be extra safe.
Fun Fact: You don’t have to actually send someone back to the Loop for them to exit it. You can exit mid-Loop through the records, once you hit a specific number that you want to Loop through.
And just like that, we were able to count and tell if we were about to hit a limit. While this was all concept, did you catch an area I should have included into this? An alert to the Admin. Throw in a Chatter Post or an Email to you for when you hit that limit, so you can review everything is still functioning as you’d expect.
Hey David. Thanks for a great overview, and tip on the counter. Do you have any advice on what to do if you want to create e.g. more than 2000 records as part of a loop (usecase is to generate opportunities for all members of a campaign)? Is there a way of calling the loop again “on itself” passing in the remaining records from the sObject Collection?
Is there a reason you’d not want this to be something that runs on Campaign Member? That seems like you can just have it run each time someone is added to the Campaign. If you’re wanting to batch it up, that get’s harder and you’d have to either break the Flow up into multiple transactions or go with Apex.