Search code examples
odooodoo-8openerp-8

How to link Leads (emails) to Marketing emails sent in Odoo v8?


I have Leads and all my leads have emails set. Now I have sent emails to leads with "Mass Mailings" from "Marketing". But now when I click on my lead I do not see any link between the Lead (email) to emails I have sent with "Mass Mailings".

Is there a way to make a link between Lead (email) to email I have sent?


Solution

  • The information about sent emails is available trough the model mail.mail.statistics. In this model you have everything you need. The following fields may be of interest for your task:

    • model - as emails may be related to any Odoo model, this field tells you to which model was related the email. You are interested in records with crm.lead in this field
    • res_id - the id of the corresponding model instance. In your case this fields links you to the id of your lead.
    • mail_mail_id_int - the id of the objects of type email.email - the emails themselves

    etc.

    You can use this to create a list of emails related to a lead and show them in the crm lead form.

    To do that, create a new Odoo module, extend the crm.lead object adding a One2many relation to the mail.mail.statistics model and extend the crm.lead view to show this new field.

    For instance, in a file called models/lead.py in this new module, put the following:

    from openerp import models, fields
    
    
    class crm_lead(models.Model):
    
        _inherit = 'crm.lead'
        emails = fields.One2many(comodel_name='mail.mail.statistics',
                                 inverse_name='res_id',
                                 domain=[('model', '=', 'crm.lead')])
    
    crm_lead()
    

    Respectively, to extend the view, create a file views/lead_view.xml like this:

    <?xml version="1.0"?>
    <openerp>
        <data>
            <record model="ir.ui.view" id="mail_crm_stats.crm_lead_form">
                <field name="name">mail_crm_stats.crm_lead.form</field>
                <field name="model">crm.lead</field>
                <field name="type">form</field>
                <field name="inherit_id" ref="crm.crm_case_form_view_leads"/>
                <field name="arch" type="xml">
                    <xpath expr="//notebook/page[@string='Extra Info']"
                           position="after">
                        <page string="Emails sent">
                            <group name="emails">
                                <div>
                                    <field name="emails" nolabel="1"
                                           class="oe_inline"/>
                                </div>
                            </group>
                        </page>
                    </xpath>
                </field>
            </record>
        </data>
    </openerp>
    

    Now you should see additional tab 'EMails sent' in your lead form. Of course, this is just and example and the module may be improved to show better information about the sent emails. As the case is interesting I may commit soon a new version in the github repository I created for the purpose..

    You can download the entire module and test it from my github repository like this:

    git clone https://github.com/andreiboyanov/odoo-mails_crm_stats mails_crm_stats