Odoo Help

Welcome!

This community 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.

1

Send a mail once a sale order is created

By
scot77844
on 7/16/15, 5:00 AM 678 views

Hi,

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"

scot77844
on 7/16/15, 5:17 AM
1
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.

1
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/

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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