Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odgovori
676 Prikazi

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

Avatar
Opusti
Best Answer

Hii,

from odoo import models, api


class CrmLead(models.Model):

    _inherit = 'crm.lead'


    def write(self, vals):

        res = super().write(vals)


        # If team was changed

        if 'team_id' in vals:

            for record in self:

                team_leader_user = record.team_id.user_id



                if team_leader_user and partner.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

try this code i hope it is use full

Avatar
Opusti
Related Posts Odgovori Prikazi Aktivnost
3
jul. 25
797
1
jun. 25
626
1
jun. 25
546
1
feb. 25
3034
1
jul. 25
405