I want to send an email notification to the user's team leader when they are added to a lead. In the create method,
@api.model
def create(self, vals):
record = super().create(vals)
body = self.env['ir.qweb']._render('mail.message_user_assigned', {'object': record})
record.message_notify(
subject=f"New Opportunity {record.name} Assigned to {record.user_id.name}",
body=body,
partner_ids=[record.team_id.user_id.partner_id.id], # recipients
email_layout_xmlid='mail.mail_notification_layout',
record_name=record.name, # optional: visible in chatter
model_description = self.env['ir.model']._get(record._name).display_name,
)
return record
the following code sends an email to the team leader. However, the below code in the write method doesn't send an email.
def write(self, vals):
res = super().write(vals)
if 'team_id' in vals:
for record in self:
# Add the team's user_id's email if it's different from the creator
if record.team_id.user_id and (record.create_uid.id != record.team_id.user_id.id):
partner_id = record.team_id.user_id.partner_id
if not partner_id:
continue
if partner_id.id not in record.message_follower_ids.mapped('partner_id').ids:
record.message_subscribe(partner_ids=[partner_id.id])
# Render the QWeb template
body = self.env['ir.qweb']._render('mail.message_user_assigned', {'object': record})
# Send notification
record.message_notify(
subject=f"Opportunity {record.name} assigned to {record.user_id.name}",
body=body, partner_ids=[partner_id.id],
email_layout_xmlid='mail.mail_notification_layout',
record_name=record.name,
model_description=self.env['ir.model']._get(record._name).display_name,
)
return res
Odoo v18 Community edition