Skip to Content
Menu
This question has been flagged

How to add a dynamic hyperlink to a lead in the CRM in an email template?

  1. I have an email sent when a new lead is created, and I want to a link to the lead.
  2. I have a One2many secondary_sales_ids field and I want to send an email (from the secondary sales model) with the lead's link when a new secondary salesperson is added.

Odoo v18 Community edition

Avatar
Discard
Best Answer

To add a dynamic hyperlink to a lead in the CRM in Odoo 18 Community Edition , and send it via email from the secondary sales model (when a new secondary salesperson is added) , follow these  steps :


STEP 1: Construct the Lead Link in the Email Template

Odoo dynamically renders links using templating language ( ${object.field} , t-esc , t-href , etc.). Here's how to insert a dynamic link to a lead:


Option A: If you're sending the email via a QWeb Email Template

Use this in your email body:

<p>
    View the Lead:
    <a t-attf-href="${object.lead_id.get_base_url()}/web#id=${object.lead_id.id}&model=crm.lead&view_type=form">
        ${object.lead_id.name}
    </a>
</p>
  • object.lead_id is the Many2one field in your secondary.salesperson model linking to crm.lead .
  • get_base_url() returns the base domain, eg, http://localhost:8069 .

Option B: If you're writing the email content manually in Python (from code)

Use this approach when sending email via code (eg, message_post , mail.template triggered by Python):

lead_url = f"{lead_id.get_base_url()}/web#id={lead_id.id}&model=crm.lead&view_type=form"
body = f"""
<p>Hello,</p>
<p>A new lead has been assigned to you.</p>
<p>Click here to view it: <a href="{lead_url}">{lead_id.name}</a></p>
"""
# Now send this body via mail.template or message_post

STEP 2: Add Email Sending Logic in create() of secondary.salesperson

In your secondary.salesperson model ( secondary_sales_ids ), override the create method to send the email when a record is added:

from odoo import models, fields, api

class SecondarySalesperson(models.Model):
    _name = 'your.secondary.salesperson.model'

    lead_id = fields.Many2one('crm.lead', string='Lead')
    user_id = fields.Many2one('res.users', string='Secondary Salesperson')

    @api.model
    def create(self, vals):
        record = super().create(vals)
        if record.lead_id:
            template = self.env.ref('your_module_name.email_template_secondary_sales_lead_link')
            template.send_mail(record.id, force_send=True)
        return record
STEP 3: Define the Email Template (in XML)
<record id="email_template_secondary_sales_lead_link" model="mail.template">
    <field name="name">Lead Assigned to Secondary Salesperson</field>
    <field name="model_id" ref="your_module.model_your_secondary_salesperson_model"/>
    <field name="subject">New Lead Assigned: ${object.lead_id.name}</field>
    <field name="email_from">${(user.email or 'noreply@example.com')|safe}</field>
    <field name="email_to">${object.user_id.email|safe}</field>
    <field name="body_html" type="html">
        <![CDATA[
        <p>Hello ${object.user_id.name},</p>
        <p>You have been assigned to a new lead.</p>
        <p>Lead: <strong>${object.lead_id.name}</strong></p>
        <p>
            Click here to open the lead:
            <a t-attf-href="${object.lead_id.get_base_url()}/web#id=${object.lead_id.id}&model=crm.lead&view_type=form">
                ${object.lead_id.name}
            </a>
        </p>
        ]]>
    </field>
</record>
Final Notes:
  • Replace your.secondary.salesperson.model with your actual model name (like crm.lead.secondary.salesperson ).
  • Replace your_module_name with the name of your custom module.


Thanks & Regards,

Email: contact@datainteger.com

Avatar
Discard
Related Posts Replies Views Activity
1
Jun 25
189
1
Jun 25
213
2
Jun 25
362
1
May 25
364
2
May 25
392