Loop
Overview
The Loop Activity feature create a loop based on the number of data objects it has in the list. For each loop, one data object in the list is executed.
How to use loop activity
- Add a loop activity to the workflow
- Data query
- Create filter
var filter = FilterBuilder.Create().Build();
- Query data object from data class based on the filter
var dataApi = ctx.Use<IDataApi>();
var result = dataApi.Search(DataObjectApiQuery
.For(<dataclassName>)
.FilterBy(filter));
- Add the Ids of all the data objects to a list
foreach(var item in result.Data)
{
data.Add(item["Id"].ToString());
}
- Return the list
return data;
- Trigger Workflow
The triggered workflow is looped until it cannot get the data.
The triggered workflow must be automatic (Automatic Workflow is checked).
- The triggered workflow will be executed for each data object the loop activity can get.
Examples
We prepared a demo for you to understand how the loop activity worked.
We have a list of people in the company (their names and years of work)! Based on their year(s) at the company, the board of directors will award:
- He/she has been working for the company for 3 or more years => $50 voucher + Valhalla medal
- He/she has been working for the company for 2 years => $50 voucher
- He/she has been working for the company for 1 year => 20$ voucher
We need to run through all the people on the list and get their years of work. After that, we will update the reward of each person.
Preparation
- Data class.
We create a data class to store employees's information such as Name, Address, YearsOfWork, Reward. You can choose your <dataclassname>. Our is Employee.
- Data objects for the data class.
You can create data object for the data class through script or UI. We create 3 employees with their years of work are 1, 2, and 3 respectively. As you can see in the picture below, they do not have their reward updated yet.
Execution
- Create a workflow with a loop activity.
- Data query - Employees.
- Build a filter to get all the employees.
Because we need all the employees, we do not add any conditions for this filter
var filter = FilterBuilder.Create().Build();
- Get Ids of all employees and return them in a list.
var dataApi = ctx.Use<IDataApi>(); var result = dataApi.Search(DataObjectApiQuery .For("Employee") .FilterBy(filter));
- Build a filter to get all the employees.
Trigger workflow.
- Create a workflow with a script activity (remember to check in the Automatic Workflow).
- Write a script to update reward information based on years of work.
var dataApi = ctx.Use<IDataApi>(); var data = ctx.Input; var dataObjectId = data["Id"]; if (data.YearsOfWork >= 3) { dataApi.Update(dataObjectId, new { Reward = "$50 voucher + Medal" }); } if (data.YearsOfWork == 2) { dataApi.Update(dataObjectId, new { Reward = "$50 voucher" }); } if (data.YearsOfWork == 1) { dataApi.Update(dataObjectId, new { Reward = "$20 voucher" }); }
Create a test in the workflow definition containing loop activity.
Check the result.
All the rewards of the employees are updated:
You can change the condition in the triggered workflow to show different results of rewards.
Conclusion
The Loop Activity is straightforward and optimized in terms of design and performance. It can help you process a list of data objects and configure it until it meets your specific needs.