Skip to Content
Menu
This question has been flagged

hi

I want know, how send automatic email when a form's state is change?

I search and one page find that someone had given such a solution:

---------------------------------------------

@api.onchange('state')
@api.one
def email_onchange_state(self):
if self.state == 'done':
    tmplt_id = self.env['mail.template'].search([("name", "=", "my_temp_onchange_state_to_done")])  # (I create "my_temp_onchange_state_to_done" template email)
    if tmplt_id:
        self.env['mail.template'].browse(tmplt_id.id).send_mail(self.id, force_send=True)
--------------------------------------------

but when I test , it does not work.( without error)

do you have a solution?


Avatar
Discard

Check how to send email on some event: https://goo.gl/z16PBZ

Best Answer

A few hints:

1. Do not use 'onchange' for that purpose, since even not saved update will lead to an email of being sent. Instead use inverse.

2. Since you are working with some object (e.g a sale order or an opportunity) it is better to use message_post of that model without trying to use mail.template.

So, you should have something like:

@api.multi
def _inverse_state(self):
"""
For sure you can change body or subject, and generate those based on a template
"""
for obj in self:
obj.message_post(body="State is changed", subject="State is changed", subtype="mail.mt_comment")

state = fields.Selection(inverse=_inverse_state) # field type might be Many2one in case of certain models

Another alternative is to rely upon tracking. Look for examples in the project module - https://github.com/odoo/odoo/blob/12.0/addons/project/models/project.py

Avatar
Discard
Author

Hi

Thanks for your help

I wanted to use "mai". so my function became: (and works fine :) )

---------------------------------

@api.multi

def _inverse_state(self):

if self.state == 'done':

tmplt_id = self.env['mail.template'].search([("name", "=", "my_temp_onchange_state_to_done")])

if tmplt_id:

self.env['mail.template'].browse(tmplt_id.id).send_mail(self.id, force_send=True)

--------------------

state = fields.Selection(inverse='_inverse_state')

------------------------------------------------------------------------------------------

Thanks

Best Answer

Hi,

 Can you please put a print statement or logger message for the tmplt_id and see whether it receives the email template, might be the email is not send or created as the execution of the code didn't enter inside the if statement.

Refer : Finding and sending e-mail templates in Odoo

Also you can check whether the onchange function is getting executed, you can test the same by putting a print statement it. If the state of the record is changed in a button click, you can override that button action and add this codes to send email in it.


For sending the email in the during the state change, you can use the automated action to send the email. For that install the module named base_automation in the database and navigate to Settings -> Technical -> Automation -> Automated Action and create a new record here by specifying the model for which the action has to be triggered, and in the field Trigger condition specify the condition and in the Action To Do select the Send Email option.


Thanks

Avatar
Discard
Author

thanks for your help

but I did not want to use 'Aoutomated Action' (because I want know it is posibble write this without automated action?)

so I used the above method.( def _inverse_state(self) )

so thanks

Related Posts Replies Views Activity
0
May 16
2295
4
Mar 23
5941
1
Jul 21
1154
5
Nov 19
9523
2
Feb 17
3776