1. Overview
The for each block in Zoho QEngine is used to repeat a defined set of actions for every entry in a collection, allowing you to handle multiple items dynamically without writing duplicate steps. You can use it to iterate over static values defined directly in your script, or arrays returned from API responses, making it easy to process data one record at a time.
You can use a for each block when your test script needs to:
Perform identical actions on a list of static or predefined data: Apply the same operation across multiple input values or records without duplicating steps. For example, looping through a list of users, client records, or transaction entries to send notifications or update roles.
Iterate through an API response: Handle arrays returned from external or internal APIs dynamically, processing each response object individually.
Process collections generated in previous steps: Iterate over outputs produced by earlier tasks within the same script, such as filtered lists, computed datasets, or values extracted from another action. For example, a test script extracts all active client IDs. Later, the for each block iterates over these IDs to assign tasks or update records.
Instead of creating separate test scripts for each record or data point, you define the logic once, and QEngine applies it to every entry in the collection. When a for each block runs, it processes one entry at a time, temporarily storing it in a variable for use within the block. The actions within the block then execute for that specific entry before moving on to the next. This approach keeps your scripts concise, organized, and adaptable.
When iterating through a map, each loop cycle retrieves one key–value pair, letting you access both the key and its corresponding value within the same iteration.
1.1 Syntax
- for each <loopVariable> in <expression>
- {
- <logic>
- }
where:
Parameter | Description |
<loopVariable> | A placeholder variable that holds the current entry from the collection during each iteration. |
<expression> | The collection you want to loop through. |
<logic> | A block of code that runs once per element. |
2. Using for each in Zoho QEngine
Example: Iterate through users and their project roles using the for each block in Zoho QEngine
This example demonstrates how to use the for each block to process a list of users retrieved from the Zoho QEngine API. The script fetches the users using an invokeUrl call and stores the response in a variable. The "myusers" array in the response contains each user's details, including their email ID and associated project roles.
The for each block iterates over every user in the "myusers" array, assigns the current user to a temporary loop variable, and then allows you to access user details dynamically. Inside the loop, we also iterate through the "project_roles" array for each user to process or log the project name and the role of the user within that project.
- response = invokeurl
- [
- url: "https://qengine.zoho.com/api/v1/zylker/myusers"
- type: GET
- connection: "qengine"
- ];
- info(response.get("responseText"));
- response_json = response.get("responseText").toCollection();
- users_list = response_json.get("myusers");
- for each user in users_list
- {
- // Example: get email of current user
- user_email = user.get("email_id");
- info("User email: " + user_email);
- // Example: iterate through the project_roles of this user
- project_roles = user.get("project_roles");
- for each role in project_roles
- {
- project_name = role.get("name");
- project_role = role.get("role");
- info("Project: " + project_name + ", Role: " + project_role);
- }
- }
where:
Parameter | Description |
| response | Stores the response returned by the invokeUrl call to fetch users from Zoho QEngine API. |
response_json | Parsed JSON object from response.get("responseText"). This contains the full API response, including the myusers array, where each element is a user object with details such as email_id, project_roles, role_category, display_name, zuid, and status. |
users_list | List of user objects retrieved from the myusers key in the API response. |
user | Loop variable representing the current user object in the for each block. |
user_email | Email address of the current user, accessed from the email_id key. |
project_roles | List of project role objects associated with the current user. |
role | Loop variable representing the current project role object in the inner for each block. |
project_name | Name of the project associated with the current user. |
project_role | Role of the user in the current project (e.g., super admin, admin). |
3. Examples
Example 1: Iterating through a List
This example demonstrates how to use a for each block to iterate through a simple list of names. The for each block assigns each entry to the loop variable name and prints it using an info task.
- names = {"Anna", "Ben", "Chris"};
- for each name in names
- {
- info(name);
- }
Example 2: Working with a Map
This example demonstrates how to use a for each block to iterate through a map of student marks. The map studentMarks contains student names as keys and their scores as values. The script uses studentMarks.keys() to loop through the keys, assigning each key to the loop variable student, and prints the student name using an info task. This shows how you can iterate over the keys of a map and process them individually in a loop.
- studentMarks = {
- "Alice": 85,
- "Bob": 90,
- "Cathy": 78
- };
- for each student in studentMarks.keys()
- {
- info (student);
- }
To access both the key and its corresponding value, you can use the get function inside the loop. The script loops through studentMarks.keys(), assigns each key to the loop variable student, then retrieves the score using studentMarks.get(student) and stores it in stud_mark. Finally, it prints both the key and value using an info task, e.g., "Alice scored 85".
- for each student in studentMarks.keys()
- {
- stud_mark = studentMarks.get(student);
- info (student + " scored " + stud_mark);
- }
- Scripts
- Test Cases