Odoo Help


Send a mail once a sale order is created

on 7/16/15, 5:00 AM 1,526 views


I want to send a mail once a sale order got created. I am using the email templates for the mail structure. I have defined the create method in the following way.

def create(self, cr, uid, vals, context=None):

if context is None:

context = {}

temp_id = 2

self.pool.get('email.template').send_mail(cr, uid, temp_id, ids,True, context=context)

return super(sale_order,self).create(cr,uid,vals,context=None)

But for that code to work, for sending the mail, I need the current record id which is getting saved(say ids). How can I get the id of the sale order which is getting created. Please help me with a solution.

Issue resolved by using "ids = super(sale_order,self).create(cr,uid,vals,context=None)" and "return ids"

on 7/16/15, 5:17 AM
Andre de Kock
On 7/16/15, 3:07 PM

You can change your code as follows:

def create(self, cr, uid, vals, cpntext=None):
    if context is None:
        context = {}

    # Call parent method
    new_id = super(sale_order, self).create(cr, uid, vals, context=None)

    # Find email template to send
    template_ids = self.pool['email.template'].search(cr, uid, [('name', '=', 'Template name')], context=context)
    template_id = template_ids and template_ids[0] or False
    if not template_id:
        raise exceptions.Warning("Could not find template!")

    # Send email
    self.pool['email.template'].send_mail(cr, uid, template_id, new_id, force_send=True, context=context)

    return new_id

So notice that "new_id" is the id of the newly created record. Also note that the "force_send=True" parameter will immediately send the email, whereas "force_send=False" will wait for a cron job that passes by every 30 minutes to send the email.

David Bertha
On 7/17/15, 4:22 AM

 You should consider to create an Automated Action : https://www.odoo.com/apps/modules/online/base_action_rule/

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 7/16/15, 5:00 AM
Seen: 1526 times
Last updated: 7/18/15, 9:34 AM