Skip to Content
Menu
This question has been flagged
2 Replies
4829 Views

I want to send an email to customer when a field selection is changed or equal to specific state.

so I have added the field in python: "laundry_status_order"

            laundry_status_order = fields.Selection([('0','In Progress'),  ('1', 'Ready To Deliver'), ('2', 'Delivered')], default='0', track_visibility='onchange')


and I have create a new function with onchange api:

   @api.onchange('laundry_status_order')

    @api.multi

    def send_mail_template(self):

# #         # Now let us find the e-mail template

           if self.laundry_status_order=='1':

               template = self.env.ref('okto_send_mail_ecolav.email_template_ecolav_landry_ready2')   

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


This function work perfectely when I call it from a button, but when I change the field "laundry_status_order", this error is displayed: (    

  File "C:\Users\hedia\Workspace\Odoo10\odoo\addons\okto_send_mail_ecolav\models\pos_order.py", line 25, in send_mail_template
    self.env['mail.template'].browse(template.id).send_mail(self.id,force_send=True)
  File "C:\Users\hedia\Workspace\Odoo10\odoo\addons\mail\models\mail_template.py", line 534, in send_mail
    values = self.generate_email(res_id)
  File "C:\Users\hedia\Workspace\Odoo10\odoo\addons\mail\models\mail_template.py", line 448, in generate_email
    res_ids_to_templates = self.get_email_template(res_ids)
  File "C:\Users\hedia\Workspace\Odoo10\odoo\addons\mail\models\mail_template.py", line 383, in get_email_template
    results = dict.fromkeys(res_ids, False)
TypeError: 'NewId' object is not iterable

any help?
Avatar
Discard
Best Answer

template = self.env.ref('okto_send_mail_ecolav.email_template_ecolav_landry_ready2')  # is a browsable record and why you need an extra line to do same thing?

change self.env['mail.template'].browse(template.id).send_mail(self.id,force_send=True) as template.send_mail(self.id,force_send=True).
And here you are accessing multi records instead of single, so use @api.one decorator or loop through self.

Also check the self.id else give self.ids and self.ids[0] or self._id.


Avatar
Discard
Author Best Answer

I didn't success to implement a mail send notification using an onchange function, so I have try another solution, based on automated actions. even, I set :

                                                    When to Run = Based on Form Modification 
                                                    On Change Fields Trigger=laundry_status_order
but I still don't get any email. I had try object.laundry_status_order, ${object.laundry_status_order}, also my_addon,pos_order.laundry_status_order, without success.

the action work fine when I set When to Run= On Update, but I receive 2-3 mails :(

Avatar
Discard
Related Posts Replies Views Activity
1
May 23
2562
3
May 18
3783
0
Aug 20
1253
0
Apr 20
3285
1
Nov 19
3596