Add contacts to mailing lists using Automated Actions

Another example of using Automated Actions to setup data in other Models (database tables).

The requirements are to add a new contact to:

  1. Mailing Contacts AND
  2. One or more Mailing Lists

We can do this because there is a many2many field on the Mailing Contact for Mailing Lists.

We therefore need to know the Record IDs of the Mailing Lists. Odoo Studio makes it simple to do that (as shown below), but the standard download tool is another option.

In this example we will use the two mailing lists with Records IDs of 1 & 2 but you can use any Mailing Lists you have created.

Automated Action

Now we have all the information we need, so we can enable Developer Mode and navigate to Settings / Technical / (Automation) / Automated Actions:

Create a new Automated Action as shown:

  • Model = Contact
  • Action To Do = Create a new Record
  • Create/Write Target Model = Mailing Contact ​
  • Trigger Condition = On Creation
  • Apply on = Email is set
    • Domain [["email","!=",False]]

Data to Write:

FieldEvaluation TypeValue
Name (mailing.contact)Python expressionrecord.name
Email (mailing.contact)Python expressionrecord.email
Mailing Lists (mailing.contact)Python expression[1,2]
Company Name (mailing.contact)Python expressionrecord.parent_id.name

Odoo 12

My first attempt at this was not optimal, and many thanks to Pierre de Giorgio, who posted a better solution in the comments.

Note that the Models have different names in Odoo 12:

  • Mass Mailing Contact (mail.mass_mailing.contact)
  • Mailing List (mail.mass_mailing.list)
  • Model = Contact (res.partner)
  • Action To Do = Execute Python Code
  • Trigger Condition = On Creation
  • Apply on = Email is set
    • [["email","!=",False]]

Python Code

contact_name = record.name
contact_email = record.email
contact_company = record.parent_id.name
ref_model = env['mail.mass_mailing.contact']
existing_records = env['mail.mass_mailing.contact'].search([('email', '=', contact_email)])
if len(existing_records) == 0:
new_entry = ref_model.create({'name':contact_name, 'email':contact_email,'company_name':contact_company,'list_ids' : [(6,0,[1,2])]})

8 thoughts on “Add contacts to mailing lists using Automated Actions

  1. Hi,
    I have a similar issue with Odoo 12, I would appreciate if you can suggest me an approach to solve it.

    What I want to accomplish is this:
    – We have a new applicant in the Recruitment module, and is tagged with ‘wordpress’.
    – The recruiter rate the applicant with 2 or 3 appreciation.

    Using an automated action, I would like to add the applicant email to a special WordPress engineers mail list, automatically when the rate occurs.

    I am using Odoo 12, so I face the same issue as the post explain.

    And on top, I cannot update the list_id attribute, because my record is an Applicant, not a Contact.

    I would really appreciate your help.

    Like

  2. For Odoo 12 you can try putting all of this is python code. This is how I did it. I also do a quick check to make sure the email address doesn’t exist before creating the new entry. If you do it as outlined in the above example, try changing the line for the mailing lists from [1,2] to [6,0,[1,2])]. That should eliminate the need for the second automated action.

    My example also eliminates the need for the second automated example.

    Hope this helps.

    contact_name = record.name
    contact_email = record.email
    contact_company = record.parent_id.name

    ref_model = env[‘mail.mass_mailing.contact’]
    existing_records = env[‘mail.mass_mailing.contact’].search([(’email’, ‘=’, contact_email)])
    if len(existing_records) == 0:
    new_entry = ref_model.create({‘name’:contact_name, ’email’:contact_email,’company_name’:contact_company,’list_ids’ : [(6,0,[1])]})

    Liked by 2 people

    1. Hi,

      Thanks for the code, it is helpful.
      By the way, I tried to adapt it for an update, not a create :

      contact_email = record.email
      existing_records = env[‘mail.mass_mailing.contact’].search([(’email’, ‘=’, contact_email)])
      if len(existing_records) > 0:
      update_entry = existing_records.write({‘list_ids’:[(6,0,[27])]})

      It works fine, because the mass mailing contact is updated with the list (id=27), but if the contact is subscribed to another list, the update write over existing record.

      Can you help me please to solve it ?

      Like

  3. Hi,

    Thank you for your solution, it is working fine for me to transfer a new contact to a specific mailing list. I have trouble updating an already existing contact in a list. I can’t update a contact in a mailing list from the automation panel (or I do not know how to do it). Any idea ?

    Thank you for your work !

    Like

  4. Hi, thanks for your help.

    How to only update a field. For example, if I update a Opportunity related with a customer, I want to automatically update a field in customer with the information in a field that I have in the current opportunity.

    Thanks

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s