# Creating records in Odoo

Creating CRM leads and other new records from incoming webhooks with the create method.

A common integration pattern is creating new records in Odoo from events in other systems - a form submitted on a website becomes a CRM lead, an order placed in an e-commerce shop becomes a sale order. This chapter shows the first case, using the Odoo.Sample connection defined in the [main Odoo chapter](https://zato.io/docs/dev/odoo/index.html).

## From a webhook to a CRM lead {#from-a-webhook-to-a-crm-lead}

The service below is mounted on a REST channel - an external system posts JSON to it and the service creates a **crm.lead** record in Odoo. The **create** method returns the ID of the new record:

```python
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class CreateLead(Service):
    name = 'demo.odoo.create-lead'

    input = 'lead_name', 'contact_name', 'email', '-phone'

    def handle(self) -> 'None':

        # Connection to use
        conn_name = 'Odoo.Sample'

        # Local alias for readability
        request = self.request.input

        # Obtain a client connected to the system
        with self.outgoing.odoo.get(conn_name).conn.client() as client:

            # Point the client to the model we want to create records in
            model = client.get_model('crm.lead')

            # Data for the new lead
            lead_data = {
                'name': request.lead_name,
                'contact_name': request.contact_name,
                'email_from': request.email,
            }

            # Phone is optional in the input
            if request.phone:
                lead_data['phone'] = request.phone

            # Create the lead and receive its ID
            lead_id = model.create(lead_data)

            # Return the ID to the caller
            self.response.payload = {'lead_id': lead_id}
```

To expose the service over REST, create a channel for it in the Dashboard under **Connections** > **Channels** > **REST**, e.g. with /api/leads as its URL path. Invoking it then looks like below:

```text
$ curl -XPOST localhost:11223/api/leads -d '{
    "lead_name": "Website inquiry",
    "contact_name": "Jane Doe",
    "email": "jane.doe@example.com"
  }'
{"lead_id": 123}
```

The same pattern works for any model - replace **crm.lead** with **sale.order**, **res.partner** or a model from a custom module, and adjust the fields accordingly.

## Learn more {#learn-more}

- [Odoo integrations](https://zato.io/docs/dev/odoo/) - Odoo models from Python services, over XML-RPC and JSON-RPC
- [Reading data](https://zato.io/docs/dev/odoo/reading-data/) - Searching, reading and field lists against any Odoo model
- [Updating and deleting](https://zato.io/docs/dev/odoo/updating-records/) - Writing to many records at once and removing them
- [Odoo calls Zato](https://zato.io/docs/dev/odoo/odoo-calls-zato/) - Automation rules and webhooks that invoke your services
- [Odoo Online](https://zato.io/docs/dev/odoo/odoo-online/) - What differs when the instance is hosted by Odoo
