> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mileapp.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# On Task Finished

## Overview

The **On Task Finished** automation event triggers when a task is completed or finished with the selected Flow. This event captures the moment when field work is done, allowing you to automate post-completion workflows, generate reports, create follow-up tasks, or integrate with external systems at the point of task completion.

## When Does This Event Trigger?

The "On Task Finished" event is triggered when:

1. A field worker marks a task as complete/finished in the mobile app
2. A user marks a task as finished in the web portal
3. A task is completed via API
4. The task's Flow matches the Flow selected in the automation configuration

<Note>
  The automation will **only** trigger if the task's Flow matches the Flow specified in the automation settings. This allows you to create Flow-specific automation rules for different completion workflows.
</Note>

## Prerequisites

Before setting up an automation with this event, ensure:

1. You have access to the Automation feature in MileApp
2. You have appropriate permissions to create and manage automations
3. You have at least one Flow configured in your organization
4. You understand the completion workflow for your tasks

<Note>
  Required permission:

  * View Automation
  * Create Automation
</Note>

## Use Cases

### 1. Sequential Task Creation

**Scenario:** Automatically create the next task in a workflow when the current task is completed.

**Configuration:**

* **Event:** On Task Finished
* **Flow:** Pickup
* **Automation Type:** Create and Assign Task
* **Action:** Create "Delivery" task with data from completed pickup

**Benefit:** Ensures seamless workflow progression without manual intervention

### 2. Completion Notifications

**Scenario:** Notify customers when their delivery or service is completed.

**Configuration:**

* **Event:** On Task Finished
* **Flow:** Delivery
* **Automation Type:** Webhook
* **Action:** Send SMS/email notification to customer with completion details

**Benefit:** Improved customer communication and satisfaction

### 3. Proof of Delivery Archival

**Scenario:** Send completion data, photos, and signatures to document management system.

**Configuration:**

* **Event:** On Task Finished
* **Flow:** Delivery
* **Automation Type:** Webhook
* **Action:** Upload proof of delivery to external storage or ERP system

**Benefit:** Automated documentation and compliance record-keeping

## Setting Up On Task Finished Automation

### Step 1: Navigate to Automation Settings

1. Log in to MileApp web portal
2. Click on **Flow** in the main navigation
3. Click on the **Automation** tab

<div align="center">
  <img src="https://mintcdn.com/mileapp-c1584fbc/jvVKvpPwiK1qABos/images/automation-tab.png?fit=max&auto=format&n=jvVKvpPwiK1qABos&q=85&s=b6493e509416761d04fe876351c975e3" alt="Automation Tab" width="600" data-path="images/automation-tab.png" />

  <p><i>Automation tab in Flow menu</i></p>
</div>

### Step 2: Create New Automation

1. Click the **New** button in the Automation page
2. The "Create Automation" dialog will appear

<div align="center">
  <img src="https://mintcdn.com/mileapp-c1584fbc/jvVKvpPwiK1qABos/images/automation-event.png?fit=max&auto=format&n=jvVKvpPwiK1qABos&q=85&s=b1cbf60faef178fd4e743c15cb7a2d93" alt="Create Automation Dialog" width="600" data-path="images/automation-event.png" />

  <p><i>Create Automation dialog with event selection showing all available automation events</i></p>
</div>

### Step 3: Configure Automation Details

Fill in the following information:

1. **Automation Name:** Provide a descriptive name (e.g., "Create Delivery Task on Pickup Completion")

2. **Event:** Select **"On Task Finished"** from the dropdown

3. **Flow:** Select the Flow you want to monitor (e.g., "Pickup", "Delivery", "Service")

4. **Automation Type:** Choose the action type:
   * **Create Task** - Automatically create a new task
   * **Create and Assign Task** - Create and assign a task to a user
   * **Create or Update Data Source** - Modify data in Data Source
   * **Webhook** - Send data to external URL
   * **Notification** - Send notifications to users

5. Configure type-specific settings based on your chosen Automation Type

### Step 4: Configure Automation Type Settings

Depending on the Automation Type you selected, configure the following:

#### For Create Task / Create and Assign Task:

* Select the **Flow** for the new task to be created
* Map fields from the completed task to the new task
* Set default values for required fields
* (For Create and Assign Task) Select the assignee or assignment rules

#### For Create or Update Data Source:

* Select the target **Data Type** to update
* Map task completion data to Data Source fields

#### For Webhook:

* **URL:** Enter the endpoint URL where task completion data should be sent
* **Header (Optional):** Add custom HTTP headers (e.g., Authorization, Content-Type)
* **Value (Optional):** Provide corresponding header values

**Example Webhook Configuration:**

```
URL: https://api.yourcompany.com/tasks/completed
Header: Authorization
Value: Bearer your_api_token_here
```

#### For Notification:

* Select **Channel** (WhatsApp notification)
* Select **Phone Number Field** from task data
* Configure **Notification Message** with completion details

### Step 5: Configure Rules (Optional)

Add conditional rules to control when the automation should execute:

* Filter by specific field workers or teams
* Apply conditions based on hub or location
* Set completion status conditions (success, failure, partial)
* Filter by time of day or completion duration

### Step 6: Save and Activate

1. Click **Submit** to create the automation
2. Ensure the automation is set to **Active** (toggle enabled)
3. Test the automation by completing a task with the selected Flow

## Webhook Payload Structure

When using the Webhook automation type with "On Task Finished", MileApp sends a POST request with the following data structure:

```json theme={null}
{
  "event": "on_task_finished",
  "timestamp": "2024-01-15T16:45:00.000Z",
  "task": {
    "id": "task_id_123",
    "flow": "Delivery",
    "flowId": "flow_id_456",
    "invoice": "INV-2024-001",
    "name": "John Doe",
    "phone": "+6281234567890",
    "address": "Jl. Sudirman No. 123, Jakarta",
    "deliveryCoordinate": "-6.2088,106.8456",
    "startTime": "2024-01-15T14:00:00.000Z",
    "endTime": "2024-01-15T16:00:00.000Z",
    "completedTime": "2024-01-15T16:45:00.000Z",
    "status": "finished",
    "completionStatus": "success",
    "duration": 9900,
    "priority": "normal",
    "proofOfDelivery": {
      "photos": [
        "https://storage.mile.app/tasks/photo1.jpg",
        "https://storage.mile.app/tasks/photo2.jpg"
      ],
      "signature": "https://storage.mile.app/tasks/signature.png",
      "notes": "Package delivered successfully"
    }
  },
  "assignee": {
    "id": "user_id_789",
    "name": "Field Worker Name",
    "email": "worker@company.com",
    "phone": "+6281234567891"
  },
  "hub": {
    "id": "hub_id_789",
    "name": "Jakarta Hub"
  },
  "completedBy": {
    "id": "user_id_789",
    "name": "Field Worker Name"
  }
}
```

<Note>
  The exact payload structure may vary based on your Flow configuration and the fields defined in your Flow components. The `duration` field is in seconds (time from assignment to completion).
</Note>

## Monitoring and Troubleshooting

### Viewing Automation Logs

To verify your "On Task Finished" automation is working correctly:

1. Go to **Flow > Automation**
2. Find your automation in the list
3. Click the **clock history** icon to view execution logs
4. Review successful executions and any errors

### Common Issues

**Issue:** Automation not triggering when tasks are completed

**Possible Causes:**

* Automation is not set to Active
* Task's Flow does not match automation's Flow configuration
* Rules/filters exclude the completed tasks
* Task status is not "done"

**Solution:**

* Verify automation is enabled (toggle is ON)
* Check that task Flow matches automation Flow setting
* Review rules to ensure they don't overly restrict triggering
* Ensure task is marked as completed with status "done" to trigger automation

**Issue:** Webhook receiving incomplete task data

**Possible Causes:**

* Flow has optional fields not filled during task execution
* Photos or signatures not uploaded properly
* Network issues during task completion
* Mobile app not fully synced before completing

**Solution:**

* Review Flow configuration to identify required vs optional fields
* Ensure mobile app has stable internet connection
* Implement data validation in webhook endpoint
* Add retry logic for incomplete data

## Related Documentation

* [Automation Introduction](/pages/automation/introduction-to-automation)
* [Automation Type](/pages/automation/automation-type/introduction)
* [On Task Created](/pages/automation/automation-event/on-task-created)
* [On Task Assigned](/pages/automation/automation-event/on-task-assigned)
* [Create Task Automation](/pages/automation/automation-type/create-task)
* [Webhook Automation](/pages/automation/automation-type/webhook)
* [Task Management](/pages/task/managing-task/introduction)

## Frequently Asked Questions

**Q: What happens if a task is completed offline?**

A: The automation triggers when the mobile app syncs the completion data to the server. There may be a delay between offline completion and automation trigger.
