LCS - Loops in actions
If you have a set of records that all need the same update or delete operation done on them in an action, you can create a loop to perform this process inside an action. The looping steps begin with the selection of a set of records that you want to loop through, followed by the looping through those records with the same action step (update, delete, etc) applied to each record.
In this example, you will be building a meal menu generation application that contains a loop to go through the items in your pantry to identify any that have recently expired so you can use those in a meal before you discover them several months or years in the future at the back of the pantry and need to throw them away.
Create An Action
1. If you are not building a loop inside of an auto-generated action (one created by the platform as a result of your dragging and dropping web components onto the canvas) you can create a new action using the button in the upper right of the Action panel on the left side of the interface:
2. Your empty action will be created after you give it a useful name in the action creation dialog:
Create A Collection Variable
- Click on the Start action step to open the configuration panel for this step. The panel will open on the right side of the screen. You will create an action variable that will be used to contain all of the records matching a set of criteria so you can loop through them in a later step.
- Click on the Action Variables tab in the configuration panel, then click on the Add button:
- Create a collection variable to contain all of the records you need to update or delete. This collection can be all of the records in a data model, or contain a limited set of those records when you use a filter so the records match a set of criteria. In this example, the data model containing a list of pantry items like cans of food are filtered for those with an expiration date older than today. Note that you can add more than one filter to retrieve a more or less limited set of records, combining the filters together with a logical And or a logical Or.
- Choose a name for this variable that is easy to remember, like ItemCollection.
- Pick the data model that contains the set of records you want to loop through.
- Use the Indexed slider if you need to create an indexed set of records in your collection. If you are returning many records (hundreds or thousands), this indexing can greatly increase the speed of your loop processing.
- Use the Skip field to select only every other record, or every third record in your data set. In most cases, leave this at the default value of 1 to take every record that matches your filter criteria.
- Use the Take field to create the total number in a batch of records to be processed at one time. In most cases, leave this at the default value of 50.
- Finally, use the Order field to choose which property of your data model will be used to order your data set, if you need to process these records in a specific order. Then choose whether you want these records to be processed in Ascending or Descending order. Ordering records is occasionally useful when calculating a subtotal or updating records until a specific value is found in the subset.
- When you are done selecting options for your collection variable, click on the Save button in the lower-right corner of the dialog window as seen here:
Add the Loop Action Step
- Drag the loop action step from the action steps pane at the left of the builder interface onto the action canvas. You will see the interface suggest locations for this step in the series of action steps by displaying a spinner in those locations. Drop the loop action on one of the spinners where you need it in the action steps:
- The action step configuration panel will automatically open after you drop the Loop on the action steps spinner.
- Update the name of your loop by clicking on the default title. Choose a name that identifies what the loop does so that the next person who works on this application can understand what the loop does just by looking at the title.
- Choose your collection variable created previously for the Array this loop will be working through.
- Add an iterator variable, named appropriately so you can identify what it does. The iterator variable will contain the complete record from the data model, so you can modify any of the record properties or use it in any other data model processes.
- Optionally, create an index variable. The index variable will contain the current index of the number of records that have been looped through, so you can run your loop a specific number of times if that is what your business requirement suggests. Note that the index is zero-based, so the first record processed will have an index value of 0.
- Your completed configuration might look similar to this:
- Save your changes, then close the configuration panel using the X in the upper right corner.
Add the Update Action Step
- Drag the Update action step from the action steps pane at the left of the action builder interface onto the action canvas. You will see the action builder interface suggest locations for this step by displaying a spinner in those locations. Note that you can also add a Delete, Create or any other action step inside your loop, but these instructions use the Update action step.
- Drop the Update action on the spinner inside your loop so it is repeated for each record in your loop collection.
- The action step configuration panel will automatically open on the right side of the interface.
- Change the name from Update Record to something more self-descriptive to differentiate this step from any other Update step.
- In the Record field, choose your previously created iterator variable. This variable will contain the complete record with all properties from the data model.
- In the Update action, you will choose which parts of the iterator you want to update using the Add button inside the Value Mapping pane. If you were using the Delete action step, you would choose the iterator variable to delete each record one by one. The Create action step contains a similar Value Mapping pane so you can create new records in your data model. In this example, the checkbox for UseSoon property will get toggled automatically to True to place these ingredients on the list for near-future menu inspiration.
- Use the As input to map the value you are updating to a variable if you need to do further processing of that value. Note that this is an optional input.
- Leave the Validates toggle (inside the Advanced pane at the bottom of the panel) activated to follow the data model validation you have created previously.
- Your completed configuration will look something like this:
- Click on the Save button at the bottom, then click on the X in the upper-right corner to close the Action Step configuration panel.
This completes your Loop configuration. Be sure to test your loop with test data from the user interface and confirm it works as you intended.