This question has been flagged
1 Reply
355 Views

The examples I have seen focus on finding an existing record in Odoo to update it.

How would I create a brand new record?

Avatar
Discard
Best Answer

Although Odoo does want you to identify an existing record in order for the Webhook to work, you don't need to update it, you can execute any code you like.

In this example, I am using the "User" model, and I am providing the "ID" of 1, which I know will always find the Odoobot User.  

The Target Record field is completed with model.browse(int(payload.get('id')))



Then, the code I execute just parses the payload and uses it to create the new record, here I am creating a new Contact (res.partner):


Note that there is no error checking in this example, it will only work if the payload provides the ID of 1 (to find the existing User record) and has a NAME so that I can create a new Contact with that name.


To test, I called the webhook via CURL:


curl -H 'Content-Type: application/json' -d '{ "id": 1,"name": "Ray Carnes (Odoo)" } ' -X POST https://webhooks.odoo.com/web/hook/e63e3705-2fba-4795-98a3-64914be0ba8b

Avatar
Discard

Hi, I get very excited when I saw this post. I followed the example Ray suggest however it does not work for me. I get: {"status": "error"}
Curl: curl -H 'Content-Type: application/json' -d '{ "id": 1,"name": "Guppy" } ' -X POST (url removed)
Error log: Webhook #1 could not be triggered because no record to run it on was found.
Info log: Webhook #1 triggered with payload {'id': 1, 'name': 'Guppy'}
Target Record: model.browse(int(payload.get('id')))
Code: try:
data = request.get_json_data()
except:
data = str(request.get_http_params())

name = data['name']

env['res.partner'].create({
'name': name,
})

I just successfully tested this today on Odoo 17.2.

Is your Automation Rule set up to use the res.user (User) model?

Post a new question that prevents guessing (how is your Automation Rule setup?) and enables reproducibility (which version?) if you still can't get it to work.