# Lesson #3: Route work with subflows

After [Lesson #1](/learn/l1-build-your-first-flow/) and [Lesson #2](/learn/l2-process-lists-with-ai/), build modular, intelligent workflows.

In this lesson, you will break down playbooks with **Subflows**, use **AI··Tasks**, and manipulate payloads with **Internal Dictionaries** and **JSONPath**.

## The Use Case: Automated Customer Support Triage

To illustrate these concepts, we will build a customer service workflow using **Typeform** and E**mail**.

**The Scenario:** When a customer submits a feedback form via Typeform, our main playbook will catch the data. It will use an AI··Task to analyze the sentiment of the message and draft a clear internal escalation email. Then, an internal dictionary will determine which department should handle the ticket based on the product category. Finally, the main playbook will trigger a Subflow to send this AI-generated summary to the correct team via Email.

---

## Prerequisites: Creating the Typeform

To ensure our Mindflow playbook works perfectly, we first need to set up the form that will act as our Trigger. Typeform's free version is more than enough for this exercise.

### Step A: Create the form structure

Log into your Typeform account, click on **Create a new form**, and select **Start from scratch**.

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

   ![Step](./assets/step.png)

   </div>

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

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

   </div>

Add the following three questions, strictly respecting the order and field types, as this will impact your JSONPath queries in Mindflow:

1. **Client Contact**
   - **Field Type:** `Email`
   - **Question:** "At which email address can we contact you?"
2. **Collecting the feedback**
   - **Field Type:** `Long Text`
   - **Question:** "Can you detail your feedback regarding your recent order?"
3. **Product Category (for routing)**
   - **Field Type:** `Multiple Choice`
   - **Question:** "What type of product does your request concern?"
   - **Mandatory choices (must be written exactly like this for our Dictionary to work):**
     - `clothing`
     - `shoes`
     - `accessories`

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

   ![Accessories](./assets/accessories.png)

   </div>

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

   ![Accessories](./assets/accessories-2.png)

   </div>

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

   ![Accessories](./assets/accessories-3.png)

   </div>

### Step B: Connect the Webhook to Mindflow

Once the form is ready and published (click the **Publish** button in the top right corner):

1. Go to the **Connect** tab at the top of the Typeform interface.
2. In the left-hand menu, click on **Webhooks** and click **Add a webhook**.

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

   ![Left hand menu click webhooks click](./assets/left-hand-menu-click-webhooks-click.png)

   </div>

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

   ![Left hand menu click webhooks click](./assets/left-hand-menu-click-webhooks-click-2.png)

   </div>

3. **Endpoint URL:** Create a new workflow in Mindflow, add a **Webhook Trigger** and paste the URL generated by the Webhook trigger of your main Mindflow playbook here.
4. Click on **Save webhook**, and make sure to toggle it **On** (the switch should turn green).

To generate test data, click on **Share**, open your Typeform link, and submit a fake ticket. Your Mindflow trigger will capture this event, and you can start configuring your flow

---

## Step 1: Data Extraction with JSONPath

When your Webhook trigger receives an event from Typeform, it captures a dense JSON "payload" containing dozens of technical data points.

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

   ![Click save webhook make sure toggle](./assets/click-save-webhook-make-sure-toggle.png)

   </div>

Instead of keeping all of it, we use **JSONPath** to isolate exactly what we need.

JSONPath is a standardized query language used to navigate through a JSON object. To apply it in Mindflow:

1. Add a **Transform Data** step immediately after your Typeform trigger.
2. Create a new variable for each element you want to extract (e.g., `email`, `review`, `type`).
3. For each variable, select the `value` variable from the Webhook Trigger then click the **`fx`** button to apply a function.

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

   ![Each variable select value variable webhook](./assets/each-variable-select-value-variable-webhook.png)

   </div>

4. Enter your JSONPath queries to target the specific data in the trigger's payload:
   - To extract the customer's email: `$.form_response.answers[0].email`
   - To extract the customer's feedback text: `$.form_response.answers[1].text`
   - To extract the selected product category: `$.form_response.answers[2].choice.label`

These extracted elements become clean variables, ready to be injected into the next steps of your flow.

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

   ![Extract selected product category](./assets/extract-selected-product-category.png)

   </div>

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

   ![Extract selected product category](./assets/extract-selected-product-category-2.png)

   </div>

---

## Step 2: Intelligent Routing with an Internal Dictionary

We need to route the ticket to the correct department based on the product category the customer selected. Instead of building a long, messy chain of "If/Else" conditions, we will use a **Dictionary**.

A dictionary works on a simple **Key / Value** system. You provide a "Key" (the product category), and it instantly returns the corresponding "Value" (the department's email address). To create this dictionary in Mindflow, we will set it up as a global variable:

1. Open the flow settings by clicking the **gear icon** (⚙️) in your Mindflow interface.
2. Navigate to the **Variables** tab. Here, you can create "global" variables that are accessible and usable throughout your entire flow.
3. Create a new variable and name it `dictionary`.
4. Set its value using the following JSON format. Copy and paste:

   ```json
   {
     "clothing": "{your_email}+clothing@{your_domain}",
     "shoes": "{your_email}+shoes@{your_domain}",
     "accessories": "{your_email}+accessories@{your_domain}"
   }
   ```

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

   ![Set its value using following json](./assets/set-its-value-using-following-json.png)

   </div>

Pro Tip: Alternatively, this dictionary creation process can also be executed directly within a **Transform Data** step on your canvas if you prefer not to use global flow variables

---

## Step 3: Adding an AI··Task (our first AI··Swarm)

Mindflow allows you to seamlessly integrate LLMs to process unstructured data without writing complex API calls.

1. Add an **AI··Task** step to your workflow.
2. Configure the **Agent** by giving it a specific behavior via a _System Prompt_. Copy and paste the following instructions:

:::note
"You are an expert customer support analyst. Your task is to read a customer's feedback and generate an internal escalation email directed to the relevant department. Your email must concisely summarize the details of the customer's request and provide an analysis of their overall sentiment (e.g., Positive, Neutral, Frustrated, Angry). Keep the tone professional and objective."
:::

1. In the Agent's prompt, insert the variable containing the customer's feedback text (extracted via JSONPath in Step 1) and the dictionary previously created.

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

   ![Agents prompt insert variable containing](./assets/agents-prompt-insert-variable-containing.png)

   </div>

2. The Agent will analyze the text and generate a structured internal email that you will pass to the final Subflow step.

We will return to this AI··Task later to give it the ability to call a sub-agent tasked with sending an email. Let's now move on to creating this sub-agent.

---

## Step 4: Modularizing with Agents (via the AI··Task)

To keep your workflows maintainable and reusable, you should use **Subflows**. A Subflow is simply another Mindflow playbook called by your main playbook. In this lesson, we will configure our AI··Task to call the Subflow directly.

To create a subflow, you must create a completely new playbook. This new playbook will use a **Flowhook Trigger**, which allows our first flow to call it securely.

## Part A: Creating the Email Subflow

1. Create a new flow named "Email Agent".
2. Add a **Flowhook Trigger** to the canvas. In the configuration of this trigger, you must define the expected input information required for the flow to execute properly. We need two inputs:
   - `Customer service email address` (the destination email retrieved from our dictionary).
   - `Customer feedback analysis` (the text generated by the AI··Task).

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

   ![Customer feedback analysis text generated ai](./assets/customer-feedback-analysis-text-generated-ai.png)

   </div>

## Part B: Connecting the Agent and Executing a Dry-Run

Before we can configure the email step in our subflow, we need to generate some test data!

1. Go back to your first flow and open your **AI··Task** (from Step 3).
2. Navigate to the **Agents** tab and add your newly created "Email Agent" flow as a capability.

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

   ![Navigate agents tab add your newly](./assets/navigate-agents-tab-add-your-newly.png)

   </div>

3. **Execute a Dry-Run** of this AI··Task. This crucial step will trigger the AI, analyze your test data, and send a real payload to the Flowhook trigger of your subflow. This data will be extremely useful for the next mapping step!

## Part C: Completing the Email Subflow

Now, switch back to your "Email Sub-Agent" flow to finish the setup:

1. Add a **Send Email** step and configure it using the elements received by the Flowhook trigger:
   - **To:** Map this to `Customer service email address`
   - **Subject:** Type `"New customer request"`
   - **HTML:** Map this to `Customer feedback analysis`

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

   ![Html](./assets/html.png)

   </div>

2. At the end of the workflow, add a **Success** step to properly terminate the execution.

## Step 5: Test Your Workflow

Everything is wired up. Submit a test response through your Typeform and watch Mindflow extract the data, generate an AI summary, route it through the dictionary, and trigger the subflow to send the final email!

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

   ![End workflow add success step properly](./assets/end-workflow-add-success-step-properly.png)

   </div>

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

   ![End workflow add success step properly](./assets/end-workflow-add-success-step-properly-2.png)

   </div>

---

**Congratulations!** You have completed Lesson #3. You now know how to extract data with JSONPath, create scalable routing logic with internal Dictionaries, inject AI··Agents to automate text analysis, and structure your processes using Subflows. You are ready to automate complex business operations!
