Topics Map > Actions

LCS - Loops in actions

Performing the same action on a set of records is more efficient if done with a programmatic loop. The Action builder creates a drag-and-drop interface with a series of configuration steps to make this happen inside the platform.

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:

Click on the   in the Actions pane at the left to add a new action to your application.

2. Your empty action will be created after you give it a useful name in the action creation dialog:

The action in its beginning state after being created by the action dialog.

Create A Collection Variable

  1. 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.
  2. Click on the Action Variables tab in the configuration panel, then click on the Add button:
    Click on the Action Variables tab, then on the  Add to create a new action variable.
  3. 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.
  4. Choose a name for this variable that is easy to remember, like ItemCollection.
  5. Pick the data model that contains the set of records you want to loop through.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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:

The collection variable configuration, shown in the variable creation dialog.

Add the Loop Action Step

  1. 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:Drag the Loop component onto the Action Canvas, dropping it on one of the spinners in the action steps.
  2. The action step configuration panel will automatically open after you drop the Loop on the action steps spinner.
  3. 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.
  4. Choose your collection variable created previously for the Array this loop will be working through.
  5. 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. 
  6. 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.
  7. Your completed configuration might look similar to this:
    The completed configuration for the Loop action step.
  8. Save your changes, then close the configuration panel using the X in the upper right corner.  

Add the Update Action Step

  1. 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.
  2. Drop the Update action on the spinner inside your loop so it is repeated for each record in your loop collection. 
  3. The action step configuration panel will automatically open on the right side of the interface.
  4. Change the name from Update Record to something more self-descriptive to differentiate this step from any other Update step.
  5. In the Record field, choose your previously created iterator variable.  This variable will contain the complete record with all properties from the data model.
  6. 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.
  7. 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.
  8. 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.
  9. Your completed configuration will look something like this:
    The completed Update action step configuration.
  10. 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.



Keywords:
iterate, looping, array, 
Doc ID:
133952
Owned by:
Jeanne H. in Low Code Solutions
Created:
2024-01-10
Updated:
2024-10-15
Sites:
DoIT Enterprise Business Systems - Low Code Solutions