Search code examples
odooodoo-8

In Odoo v8, how can I have a function called when a record is created in a given table?


I am trying to automatically create a contract, whenever some product is sold. I added a field to the product, pointing to a template contract.

I know about @api.depends, but that relies on creating a field.function. I don't need a field, but still want my function called. How can I accomplish this?


Solution

  • You need to override write method of the product and inside you can call your function like this,

    def write(self, cr, uid, ids, vals, context=None):
        # add your custom code here
        return super(class_name,self).write(cr, uid, ids, vals, context=context)
    

    You need to check your field in vals, your field (I think state field) will available if the value of this field has been changed, check as follow.

    if vals.get('state',False):
       ## call your function
    

    Remember you will get only those fields inside vals which are updated.

    Hope this helps.