raarts
raarts

Reputation: 2961

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?

Upvotes: 1

Views: 1745

Answers (2)

yucer
yucer

Reputation: 5049

You can also give a look to the auditlog module code from OCA, they have implemented an action to log the records...

But if you want to make it flexible, customizable and avoid hardcoding you better use action rules ...

Action rules allow you to bind actions to object creation and update. If you want to hook other methods here is a possible approach proposed to a similar problem (log deletes):

Upvotes: 1

Viraj Joshi - Emipro
Viraj Joshi - Emipro

Reputation: 219

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.

Upvotes: 0

Related Questions