Help

4

How to trigger function on object write?

Avatar
Allison

I want to send a message to the salesperson assigned to an Account when that account is edited by another user. How to I tie into the ORM to trigger my function on object save? The closest I have gotten is the on_change method on specific fields; I don't want to add this per field.

Avatar
Discard
4 Answers
3
Avatar
Andreas Brueckl
Best Answer

You can override the write() method of the desired object (e.g. account.account).

def write(self, cr, uid, ids, vals, context=None):
    my_custom_send_mail_function()
    res = super(my_class, self).write(cr, uid, ids, vals, context=context)
    return res

But this is a very deep intrusion into the system and may have side effects.

Mabye you can also use Automated Actions for this purpose. See menu "Settings / Technical / Automated Actions".

3 Comments
Avatar
Discard
Avatar
Yug Faa
-

Exactly, it's the best answer +1

Avatar
priyankahdp
-

Thanks Andreas..If we need to add many2one field (that data need to load after function trigger )

ex : select * from hr_department where xxxxxx then how to get them in my that field .?

Avatar
Andreas Brueckl
-

If you want to set a certain value when an object is written, you can use the provided function and update the vals dict before the call of the write().

1
Best Answer

You should not develop custom modules for such customizations. Use the base_action_rule module that allows to create such a rule in just a few clicks.

1 Comment
Avatar
Discard
Avatar
Justin
-

Is it possible to use Automated Actions to trigger client-side wizards/popups? For example, I'm trying to add the ability to detect potential duplicate Partners as a new one is entered (rather than relying on deduplication after the face). You can't trigger a wizard from inside an onchange() where this functionality would be the most useful, but I might be able to trigger a popup when the user attempts to Save a record that could be a duplicate. I've attempted to test this by using Automated Actions to link Partner Create or Write with the Deduplicate Contacts action, but nothing seems to happen.

0
Avatar
gayathiri
Best Answer

Thanks Andreas, that's the solution I found too - was hoping to not have to go there. I'll look into the Automated Actions.

Avatar
Discard
0
Avatar
Allison
Best Answer

The solution I found is overwriting the object in questions write() function. I was hoping there was a flag or setting instead, but this does the trick too.

def write(self, cr, uid, ids, vals, context=None):
    # Function calls and such here
    return super(sale_order, self).write(cr, uid, ids, vals, context=context)
Avatar
Discard