When building a process in Creatio, the ‘Read Data’ element allows you to read a Collection of records that meet defined criteria. Once you have a Collection, what can you do with it? Often, you may want to loop through each record in that Collection, taking some action(s) with each one.
In order to loop through the data, start by creating a couple of parameters in your process. One parameter should be an Integer that you’ll use to track which item in the Collection you are currently working with. I like to call this parameter “Counter”. You must set a default starting value for this parameter to be 0.
The 2nd parameter you’ll need will be a lookup parameter. This parameter should be a lookup to the object of the data in your Collection. In my example, I’m working with a Collection of contacts, so my lookup will be to the Contact object. This parameter should default to blank; we’ll fill it in with the contact we are currently working with each time we loop through the data.
Once you have your parameters set up, you can setup your process start event and any other steps leading up to the Read Data element to read your Collection as usual. When you get to the point of reading the Collection, make sure to update the Mode from the default ‘Read the first record in the selection’ to use ‘Read Collection of records’ instead.
Then, set up your filter conditions to find the set of data you want to include. Finally, I set it up to read only the Id column. (In my experience, using this approach for looping causes errors when I try to reference any other fields in the Collection. So, if I need other data on the records in the Collection, I get that information later in the process – more details below.)
Now, we are ready to start our loop. I like to use an Exclusive Gateway element to help make this clear on the process diagram.
We need to set up our flows so that the process continues looping through the Collection until it’s processed each record, and then exits the loop once it’s done. We’ll do that by using the Counter parameter to track where we are in the Collection and comparing that to the number of records in the Collection. I like to make going into the loop my default flow, and exiting the loop a conditional flow, but you can do it either way. Here’s what the formula on the exit flow might look like – just replace ‘Counter’ with the name of your integer parameter and replace ‘Read Contacts’ with the name of your read data element (where you read the Collection).
Within the loop itself, the flow will look something like this:
- Update the lookup parameter to hold the Id of the record you are currently processing
- Do whatever you need to do with that record
- Update the integer parameter to track the fact that you’ve completed processing that record and are ready to move on to the next record in the Collection (if any)
First, you’ll need to use a Formula element to set the value of your lookup parameter. Use the example below—again replacing ‘Counter’ with the name of your integer parameter—and replacing ‘Read Contacts’ with the name of your read data element.
What comes next in your process will vary widely from one process to another. This is where you do whatever it is that you need to do with each record in your Collection. If you need any other data from the record you’re currently processing, use a Read Data element, passing in the Id from your lookup parameter.
Once you’ve added all of the steps to complete the necessary actions, you need one last step before you complete the loop back to the Gateway element. This last step is to update the integer parameter, increasing it by one so that the next time through the loop moves on to the next record in the Collection. To do this, use another Formula element. Again, you can use the image below as an example, replacing ‘Counter’ with the name of your integer parameter.
After that, just connect back to your Gateway and you should be all set. If you need to do any actions after the looping is completed, add them in between the ‘Done looping’ conditional flow and the End element.
One last thing to watch out for: There is a setting in the Read Data element to set a maximum number of records to include in your Collection. There is also a setting in the overall Process Settings for ‘Maximum Number of Repetitions’, which is the maximum number of times the process will go through the loop. The default for ‘Maximum Number of Repetitions’ is 100, so if you are reading more than 100 rows in your Collection, you’ll need to update this setting to match.
Finally, this blog wouldn’t be complete without mentioning that sometimes, looping through the Collection in this way isn’t the best way to process the Collection. You can pass a Collection into a Sub-Process, processing each record in the Collection as a separate instance of the sub-process. This is often a better approach, but I’ve come across some process needs where that approach just won’t work for various reasons. That’s when I turn to this solution of creating a loop directly within my Process.