# Lesson #2: Process lists with AI

In [Lesson #1](/learn/l1-build-your-first-flow/), you built a linear flow. Real-world operations are rarely linear — they involve lists of data, complex parsing, and smart decisions.

In this lesson, you will build a **Football News Researcher**. This flow takes a list of football leagues, uses an **AI··Task** to search the web via **Tavily**, and emails you a refined summary of the findings.

## Prerequisites

- Completion of [Lesson #1: Build your first flow](/learn/l1-build-your-first-flow/).
- A [Tavily](https://www.tavily.com) API key (integrated into Mindflow).

---

## Step 1: Schedule your flow

**Add a Scheduler Trigger** and set it to run weekly every Monday at 8:30 AM

   <div class="docs-screenshot-compact">

   ![Tavily api key integrated into mindflow](./assets/tavily-api-key-integrated-into-mindflow.png)

   </div>

## Step 2: Handling Multiple Inputs with "For Each"

Automation often starts with a list. Instead of running a flow five times for five different topics, we use a **For Each** loop to process them all in one go.

1. **Add a variable:** Create an array (list) of topics you want to research (e.g., `["Premier League", "LaLiga", "Ligue 1"]`).

   <div class="docs-screenshot-compact">

   ![Add variable](./assets/add-variable.png)

   </div>

1. **Add the "For Each" step** onto your canvas.
1. **Map the Input:** Select your list of `Leagues` as the source. Mindflow will now execute every step inside this block for **each item** in your list.

   <div class="docs-screenshot-compact">

   ![Map input](./assets/map-input.png)

   </div>

---

## Step 3: Empowering AI with Tool Calls (Tavily)

An **AI··Task** is powerful, but an **AI··Task** with **Tools** is a game-changer. Here, we give the AI the ability to "search the web."

1. **Add an AI··Task step** inside your loop.
2. **Select the Model:** Choose a model that supports Tool Calling (e.g., GPT-5.2 or Claude 4.5).
3. **Assign the Tool:** In the "Tools" section of the step, select the **Tavily Search** tool. Assign the Tavily credential you added as a requierements and select allowed operations.

   <div class="docs-screenshot-compact">

   ![Assign tool](./assets/assign-tool.png)

   </div>

   <div class="docs-screenshot-compact">

   ![Assign tool](./assets/assign-tool-2.png)

   </div>

   <div class="docs-screenshot-compact">

   ![Assign tool](./assets/assign-tool-3.png)

   </div>

4. **The Prompt:** Instruct the AI:

```json
You are a research assistant. Use the Tavily tool to find the 3 most recent news articles about the following topic: {{item}}.
Here the expected json schema:
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "topic": {
      "type": "string",
      "description": "The main research topic."
    },
    "search_parameters": {
      "type": "object",
      "properties": {
        "source": { "type": "string" },
        "query": { "type": "string" },
        "time_range": { "type": "string" },
        "max_results": { "type": "integer" },
        "search_depth": { "type": "string" },
        "retrieved_at_utc": { "type": "string", "format": "date-time" }
      },
      "required": ["source", "query", "max_results"]
    },
    "most_recent_articles": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "published_date_utc": { "type": "string", "format": "date-time" },
          "title": { "type": "string" },
          "publisher": { "type": "string" },
          "url": { "type": "string", "format": "uri" },
          "summary": {
            "type": "string",
            "description": "A concise paragraph summarizing the article's main findings."
          }
        },
        "required": ["title", "publisher", "url", "summary"]
      }
    }
  },
  "required": ["topic", "most_recent_articles"]
}
```

:::note
**How it works:** The AI won't just hallucinate an answer; it will realize it needs information, call the Tavily API, read the results, and then formulate its final response.
:::

   <div class="docs-screenshot-compact">

   ![Prompt](./assets/prompt.png)

   </div>

---

## Step 4: Parsing Data with JSONata

The output from an AI··Task or an API can be messy. To extract just the specific "Summary" and "URL" you need for your email, we use **JSONata** function.

1. **Add a Transform data** step onto the canva and create a variable `Filtered-Result`. The value of this variable is the answer of the previous AI··Task
2. Use a JSONata function to pinpoint the data.
   - _Example:_

   ```json
   most_recent_articles.{
    "url": url,
    "summary": summary
   }
   ```

   <div class="docs-screenshot-compact">

   ![Step 4 example](./assets/step-4-example.png)

   </div>

**Why use this?** It ensures your final email doesn't contain technical "garbage" or metadata, just the clean information your recipient needs.

1. **Configure ForEach iteration output:** To preserve each iteration's result for use after the loop completes, configure an **iteration output** in your ForEach step. Create a new variable and map it to the `Filtered-Result` variable from the current iteration.

   <div class="docs-screenshot-compact">

   ![Configure foreach iteration output](./assets/configure-foreach-iteration-output.png)

   </div>

---

## Step 5: Sending the Resulting Intelligence

The final step is to communicate the findings.

1. **Add an AI··Task step:** This step has one mission, transform the retrieved content into clean, structured HTML.

   Instruct the AI with this prompt:

   ```json
   Provide a detailed sum of the following articles about {{Leagues}}.
   Answer, as a newsletter, with a nicely crafted HTML content.
   Include embedded URLs of selected articles.
   Only reply with the html, no backticks encapsulation.

   News: {{ForEach.Output}}
   ```

   <div class="docs-screenshot-compact">

   ![Add ai task step](./assets/add-ai-task-step.png)

   </div>

1. **Add the "Send Email" step**.
1. **Configure the Recipient:** Set your email address.
1. **Configure the Subject:** `⚽ European Football Weekly`
1. **Body:** Map the transformed output from your AI··Task into the email body.

   <div class="docs-screenshot-compact">

   ![Body](./assets/body.png)

   </div>

1. Your **Send Email** step is configured. You can Dry run it to send the email. Stay close to your inbox and you should receive an email that is the HTML anwer from the AI··Task!

   <div class="docs-screenshot-compact">

   ![Your send email step configured](./assets/your-send-email-step-configured.png)

   </div>

## Second Flow created, configured, and Scheduled

- Congratulations! You've just built your second automated flow on Mindflow.
- This flow will **trigger itself every Monday morning**, search for the latest news about European football leagues, **intelligently select** the most relevant articles, and deliver a beautifully formatted HTML newsletter straight to your inbox.
- **Ready to see it in action?** Let's run it end-to-end together. Simply click the **Play icon** in the top left corner of the canvas, or press `cmd + P` (`ctrl + P` on Windows).

   <div class="docs-screenshot-compact">

   ![Ready see it action](./assets/ready-see-it-action.png)

   </div>
