I'm trying to create a flow that is auto-launched from process builder that creates opportunity contact roles. Specifically, I'm passing in the opportunity Id and contact Id from the opportunity and I want to create three new contact role records assigning each new record the same contact but different roles. Currently, I'm just trying to assign the same role just to get it working. I'm trying to start the loop using an collection variable (number) and assigning to a loop counter variable (number). The flow is not working. I'm missing something in how I start the loop and get the flow to fire. I've provided a screen shot of the current flow layout.


2 Answers 2


Unfortunately, the flow Loop element doesn't work that way. The correct method is to build a three-part loop, which has an initialization Assignment, a Decision branch to know when to stop, and an increment Assignment to increase the counter value.

Flow Counting Set Times

As you can see, step 1 initializes Counter to 0, then we head into a decision where Counter is less than 3. If so, we enter the Loop branch, which does whatever logic you like, then adds 1 to Counter. This repeats until Counter equals 3, at which point it continues to the Continue branch.

The equivalent version of this in Apex Code would be:

for(Integer counter = 0; counter < 3; counter++) {
  // Do logic here
  • thanks you for the answer,my task is as same above , insted of checking counter>3 ,is it possible to check for a variable , like a number field from record . Ex : counter> numberField
    – Sagar Raut
    Nov 29, 2018 at 1:12

The above solution gets all the more simplified with Salesforce's latest release.

Now looping through a collection of records, incrementing the counters is no longer needed. Instead the record count can be easily fetched in a sobject collection variable and assigned to a number variable. No more need to loop around to fetch the count. Get it directly in a number variable and play around with it!

So to answer this question, all you simply need to do is like:

  1. Perform a fast lookup with your required filters enter image description here

  2. Assign the record count to a sObject collections variable. (in screenshot: nofCases)

  3. Use a Assignment step, to assign the variable to a number variable. (Point to note: the assignment variable should be of type number only to make use of equals count operator. In the screenshot, this is called testCount) enter image description here

  4. Voila! Now play the hell around with the assigned variable :)

  • 1
    Welcome to SFSE. Please don't ask for best answer or upvotes here - it is frowned upon. The asker and the community will provide that feedback where appropriate.
    – David Reed
    Dec 11, 2018 at 0:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .