This question has been flagged
3 Replies
4742 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.

Avatar
Discard
Author

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

Best Answer

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.

Avatar
Discard
Best Answer

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

Avatar
Discard