Skip to Content

Contributors

db not yet committed when overriding create/update.

Hello,
I send a webhook when an event occurs in Odoo.
The problem I have is that when overriding create/update; and triggering my webhook, the data is not yet committed in the db.
(the external system is sending in it's response that the record is not found in the case of create and get the old field value -before changes- for an update).

I tried to use env.cr.flush() and/or env.cr.commit() both in my automated action and in my code before the webhook but nothing seems to work.
Any other way I could check?
Thank you
 --
Yves Goldberg
--


by Yves Goldberg - 11:46 - 21 Jun 2024

Follow-Ups

  • Re: db not yet committed when overriding create/update.

    Hi Yves,

    flush wouldn't work, but commit should do it! But I wonder if the automated action is using the same cursor as the main process.

    Maybe a nice way could be to install OCA's "queue_job" and in your automated action, call the webhook in a separate function that you call using .with_delay().some_function()

    Then it will be executed as a queue.job, and you are sure that whenever it's picked up the original action will already have finished (or if it failed, and the cr is rolled back, then the webhook won't be called at all since the queue job record will also not have been created, which is also a feature you won't have if you call the webhook directly)

    cheers


    On 6/21/24 11:48, Yves Goldberg wrote:
    Hello,
    I send a webhook when an event occurs in Odoo.
    The problem I have is that when overriding create/update; and triggering my webhook, the data is not yet committed in the db.
    (the external system is sending in it's response that the record is not found in the case of create and get the old field value -before changes- for an update).

    I tried to use env.cr.flush() and/or env.cr.commit() both in my automated action and in my code before the webhook but nothing seems to work.
    Any other way I could check?
    Thank you
     --
    Yves Goldberg
    --

    _______________________________________________
    Mailing-List: https://odoo-community.org/groups/contributors-15
    Post to: mailto:contributors@odoo-community.org
    Unsubscribe: https://odoo-community.org/groups?unsubscribe


    by Tom Blauwendraat - 11:56 - 21 Jun 2024
  • Re: db not yet committed when overriding create/update.
    You can send your webhook using a callback on the postcommit callback.
    Check in the Odoo code for the use of the decorator @self.env.cr.postcommit.add

    class BaseCursor:
        """ Base class for cursors that manage pre/post commit hooks. """

        def __init__(self):
            self.precommit = tools.Callbacks()
            self.postcommit = tools.Callbacks()
            self.prerollback = tools.Callbacks()
            self.postrollback = tools.Callbacks()

    --
    Yann PAPOUIN, Ingénieur R&D | DEC


    Le ven. 21 juin 2024 à 11:48, Yves Goldberg <notifications@odoo-community.org> a écrit :
    Hello,
    I send a webhook when an event occurs in Odoo.
    The problem I have is that when overriding create/update; and triggering my webhook, the data is not yet committed in the db.
    (the external system is sending in it's response that the record is not found in the case of create and get the old field value -before changes- for an update).

    I tried to use env.cr.flush() and/or env.cr.commit() both in my automated action and in my code before the webhook but nothing seems to work.
    Any other way I could check?
    Thank you
     --
    Yves Goldberg
    --

    _______________________________________________
    Mailing-List: https://odoo-community.org/groups/contributors-15
    Post to: mailto:contributors@odoo-community.org
    Unsubscribe: https://odoo-community.org/groups?unsubscribe


    by Yann Papouin - 11:56 - 21 Jun 2024