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

# Create or Update Data Source

> Write a finished task's values into a data source record, creating it or updating the existing one.

**Create or Update Data Source** writes the values of a finished task into a [data source](/pages/workflow/data-source/introduction). If a record with the same primary key already exists, it is updated; otherwise a new record is created. Field work keeps your master data current: customer addresses, stock counts, service history, sales leads.

Available for: **On Task Finished** only, because it needs the task's complete data.

<Note>
  Required permission:

  * View automation
  * Create automation
</Note>

## Automation detail

<div align="center">
  <img src="https://mintcdn.com/mileappv4/Xul_B0hUk35UiMLk/images/v4/workflow/automation/action-create-or-update-data-source.png?fit=max&auto=format&n=Xul_B0hUk35UiMLk&q=85&s=9194031c56e9dd9904fb0e13c9914c21" alt="Create or Update Data Source detail" width="600" data-path="images/v4/workflow/automation/action-create-or-update-data-source.png" />
</div>

1. **Data Type**: required. The [data type](/pages/workflow/data-type/introduction) whose records are created or updated, for example **Store**.
2. **Field Mapping**: every field of that data type. For each one, type a fixed value or click the search icon to copy a field of the finished task, exactly as in [Create Task](/pages/workflow/automation/actions/create-task#filling-a-field). The data type's primary key field is marked **(primary key)**.
3. **Data Source Access**: shown only for data types whose records can be limited to certain users. Tick it to control who can see the records this automation writes, then choose how:
   * **Update**: add the chosen users to the record's existing access list.
   * **Replace**: replace the record's access list with the chosen users.
4. **Limit Data by User**: who gets access. Either one option from **Auto limit data based on**, which takes the person from the task when it runs:

   * **Same assignee**: the user assigned to the task.
   * **Assigned by**: the user who assigned it.
   * **Done by**: the user who finished it.

   or one or more named users from the **User** group.

## How it works

1. A task of the automation's task type is finished, and the [rules](/pages/workflow/automation/rules), if any, are checked.
2. The value mapped to the **primary key** is looked up in the data type.
3. **A record with that key exists**: it is updated with the mapped values. Fields that aren't mapped keep their values.
4. **No record has that key**: a new record is created with the mapped values.

<Note>
  * Map a value to the primary key field. Without it, records can't be matched.
  * The primary key is matched exactly, including upper and lower case.
  * Required fields of the data type must get a value, fixed or copied.
  * The data type's field validation applies.
</Note>

## Examples

**Customer database after delivery**

| Setting | Value |
| - | - |
| Event | On Task Finished |
| Task Type | Delivery |
| Data Type | Customer Database (primary key: Customer Name) |
| Customer Name | Customer Name (from Delivery) |
| Customer Address | Customer Address (from Delivery) |
| Customer Coordinate | Delivery Coordinate (from Delivery) |
| Customer Phone | Customer Phone (from Delivery) |

**Inventory after a stock check**

| Setting | Value |
| - | - |
| Task Type | Stock Inspection |
| Data Type | Inventory Database (primary key: Product ID) |
| Product Name | Inspected Product |
| Current Stock | Stock Count |
| Warehouse Location | Inspection Location |

**Service history**

| Setting | Value |
| - | - |
| Task Type | Home Cleaning |
| Data Type | Service History (primary key: Service ID) |
| Customer Name | Customer Name |
| Service Type | Service Category |

**Sales leads from canvassing**

| Setting | Value |
| - | - |
| Task Type | Field Canvassing |
| Data Type | Sales Leads (primary key: Prospect Name) |
| Contact Number | Phone Number |
| Interest Level | Survey Result |
| Data Source Access | Update, Limit Data by User: Done by (the canvasser sees their own leads) |

## Good practice

* Choose a primary key that really is unique, such as a customer ID or order number.
* Prefer copied values over typed ones, and map only fields that matter.
* Use [rules](/pages/workflow/automation/rules) to write only when it makes sense, for example only successful deliveries.
* Test with a sample task, then check the record on the Data Source page and the run in the [log](/pages/workflow/automation/log).

## Troubleshooting

**No record is created or updated.**

* Check that the automation is **Active**, the task type matches, and the rules don't exclude the task.
* Check that the field mapped to the primary key had a value in the task.
* Check that every required field of the data type has a value.

**A new record is created every time instead of updating.** The primary key value doesn't match the existing record exactly (case included), or the wrong field is mapped to it.

**Some fields don't change.** They're not mapped, or the task's field was empty.

**The wrong data type is updated.** Edit the automation and check **Data Type**; look for other automations on the same event.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.