简体   繁体   中英

How to get new record Id in odoo?

I have created a new record and I wanted to get ID of that record. I have a method in which I want to get the ID of the newly created record. If it is true, I want to do a certain action.

@api.multi
def task_send_mail(self):   
    template_email = self.env["mail.template"].search([('name','=','Example e-mail template')]).id
    self.env["mail.template"].browse(template_email).sudo().send_mail(self.id, force_send=True)

This is my send email function. I want to send email automatically after creating the record. So, I want to get recently created record ID and pass that ID to this function.

@api.multi
def get_full_url(self):
    self.ensure_one()
    base_url = self.env["ir.config_parameter"].get_param("web.base.url")
    url_params = {
        'id': self.id,
        'action': self.env.ref('bhuwan_module.tender_records_form').id,
        'model': 'tender.manage',
        'view_type': 'form',
        'menu_id': self.env.ref('bhuwan_module.tender_records').id,
    }
    params = '/web#%s' % url_encode(url_params)
    return base_url + params

This function is called from my email template. It should takes the ID of newly created record but It is not getting those ID so rendering template is failed.

It should takes the ID of newly created record

But you didn't shared the code where it handles the newly created recordset for that model.

To get the record ID of the newly created records, you can use the create function as other mentioned in the comment. As create function of the Models definition is always fired whenever a new model is created.

@api.model
def create(self,vals):
  record = super(ClassName, self).create(vals)
  record.task_send_mail() ##record is the newly created record
  return record

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM