Creating records in Odoo
Turning incoming webhooks into CRM leads and other new records with the create method.
A common integration shape is turning events from other systems into new records in Odoo - 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.
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:
# -*- 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:
$ curl -XPOST localhost:17010/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.