For Each Loop
For each is used to perform similar set of activities on same payload of a collection. For each will expect collection as input payload and it will iterate over the collection
Point to remember
- If it’s a Java collection then it will split the payload by default
- Changes done to a payload within iteration will not change anything in the input payload
- For each work in sequential only. So if there is any error processing any record then it will not process the further records and break the look immediately
- To overcome this problem we can have try and catch block with on error cont. to keep on processing further records in case of any error
- Order will be maintained while processing
Create mule application which will accept the JSON collection as payload
{ "Employees": [ { "userId": "rirani", "jobTitleName": "Developer", "firstName": "Romin", "lastName": "Irani", "preferredFullName": "Romin Irani", "employeeCode": "E1", "region": "CA", "phoneNumber": "408-1234567", "emailAddress": "[email protected]" }, { "userId": "nirani", "jobTitleName": "Developer", "firstName": "Neil", "lastName": "Irani", "preferredFullName": "Neil Irani", "employeeCode": "E2", "region": "CA", "phoneNumber": "408-1111111", "emailAddress": "[email protected]" }, { "userId": "thanks", "jobTitleName": "Program Directory", "firstName": "Tom", "lastName": "Hanks", "preferredFullName": "Tom Hanks", "employeeCode": "E3", "region": "CA", "phoneNumber": "408-2222222", "emailAddress": "[email protected]" } ] }
Add a logger which will print the received payload
Now add For Each activity
Where following parameters are passed
- Collection – input payload which should be of type collection
- Counter variable – counter which will hold the value of current loop
- Batch Size – we can break the payload in our desired size in case we want to process more than 1 record in each loop
- Root Message – will hold the actual input payload
In our case – payload.Employees will hold the collection
Add logger to print each payload
Run the application to see the results
First logger – will print the actual input payload
INFO 2020-04-13 12:29:20,306 [[MuleRuntime].cpuLight.15: [for-each-sample].for-each-sampleFlow.CPU_LITE @4b51d624] [event: 4c7bad41-7d54-11ea-8bca-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: received payload { "Employees": [ { "userId": "rirani", "jobTitleName": "Developer", "firstName": "Romin", "lastName": "Irani", "preferredFullName": "Romin Irani", "employeeCode": "E1", "region": "CA", "phoneNumber": "408-1234567", "emailAddress": "[email protected]" }, { "userId": "nirani", "jobTitleName": "Developer", "firstName": "Neil", "lastName": "Irani", "preferredFullName": "Neil Irani", "employeeCode": "E2", "region": "CA", "phoneNumber": "408-1111111", "emailAddress": "[email protected]" }, { "userId": "thanks", "jobTitleName": "Program Directory", "firstName": "Tom", "lastName": "Hanks", "preferredFullName": "Tom Hanks", "employeeCode": "E3", "region": "CA", "phoneNumber": "408-2222222", "emailAddress": "[email protected]" } ] }
For Each Logger
First iteration
INFO 2020-04-13 12:29:20,316 [[MuleRuntime].cpuLight.15: [for-each-sample].for-each-sampleFlow.CPU_LITE @4b51d624] [event: 4c7bad41-7d54-11ea-8bca-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for each { "userId": "rirani", "jobTitleName": "Developer", "firstName": "Romin", "lastName": "Irani", "preferredFullName": "Romin Irani", "employeeCode": "E1", "region": "CA", "phoneNumber": "408-1234567", "emailAddress": "[email protected]" }
Second iteration
INFO 2020-04-13 12:29:20,318 [[MuleRuntime].cpuLight.15: [for-each-sample].for-each-sampleFlow.CPU_LITE @4b51d624] [event: 4c7bad41-7d54-11ea-8bca-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for each { "userId": "nirani", "jobTitleName": "Developer", "firstName": "Neil", "lastName": "Irani", "preferredFullName": "Neil Irani", "employeeCode": "E2", "region": "CA", "phoneNumber": "408-1111111", "emailAddress": "[email protected]" }
Third iteration
INFO 2020-04-13 12:29:20,319 [[MuleRuntime].cpuLight.15: [for-each-sample].for-each-sampleFlow.CPU_LITE @4b51d624] [event: 4c7bad41-7d54-11ea-8bca-9aaf65ed66d8] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: within for each { "userId": "thanks", "jobTitleName": "Program Directory", "firstName": "Tom", "lastName": "Hanks", "preferredFullName": "Tom Hanks", "employeeCode": "E3", "region": "CA", "phoneNumber": "408-2222222", "emailAddress": "[email protected]" }
Sample application – for-each-sample
Sample SOAP UI application – For-Each-Sample-soapui-project
so can we see the output for foreach on postman by using setpayload?