Loops execute triggers logic multiple times in a sequential order. There are two types of loops available in Caspio triggered actions: the WHILE... DO... loop and the FOR EACH RECORD IN A TABLE... DO... The former loop is used to iterate through the set of records based on a certain condition while the latter is used to iterate through all the records in a specified table. 

WHILE... DO... 

This loop evaluates if the condition (WHILE) is true and defines the action to be executed (DO). This action will be performed until the defined condition or expression becomes false. 

This trigger creates a report table that calculates the number and total amount of closed successful sales for the last 30 days. The WHILE loop is used to iterate through the past 30 days, with the day date and metrics being calculated for each iteration.  

FOR EACH RECORD IN A TABLE... DO... 

This loop defines the actions that should be executed (DO) for each record in a given table, query (SELECT block) or table variable (FOR EACH RECORD). It can be used for bulk actions to get each record that is inserted or updated in bulk (bulk update, insert through the DataPage, or with API).

Avoid using loops for bulk operations that update all records in a table. Instead, utilize the UPDATE action block without a WHERE clause to apply updates to all records within the table. 

In the above example, the trigger logic sends every CRM employee their top 5 deals. Since the top 5 deals are unique for each employee, an email with different content will be sent to each of them.  

  • Table Variable is used to store details of the top 5 deals. 
  • The FOR EACH loop is used to iterate over the Employee table. Inside this loop, there are selected the top 5 deals from the Sales Log table related to the current user (inside iteration). Then, the table variable value is used in the email body. 

Note: IF, WHILE DO, and FOR EACH blocks are resource-hungry, which means that they may slow down your application.