Odoo Help

2

add followers automatically

By
sayed fathy
on 12/17/13, 5:27 PM 5,768 views

Hello in crm.lead the responsible user (user_id) is automatically added as follower to the new task. How can I add this functionality to add some users to my custom object?

Write to the message_follower_ids field.

Gopakumar N G
on 12/18/13, 2:57 AM

Please, can you explain more!

sayed fathy
on 12/19/13, 4:52 AM

You can overwrite the create function and inject the ids on the message_follower_ids

OMNIA SOLUTIONS S.n.c. di Boscolo Matteo & C., Matteo Boscolo
on 12/20/13, 7:10 AM

it returns false

sayed fathy
on 12/22/13, 2:37 PM

In your custom module, add a field like this: 'user_id': fields.many2one('res.users', 'Anyname', select=True, track_visibility='onchange'), Then create a record and click Save. The user you specified in user_id field will automatically added on the list of followers

Aldennis
on 5/4/14, 8:37 AM

What if i want to add follower not user_id field or the parent of the user_id field

sayed fathy
on 5/5/14, 10:12 AM
0

Ray Carnes

--Ray Carnes--
19358
| 9 7 9
Keyport, United States
--Ray Carnes--

Senior ERP Analyst

OpenERP 6.1, 7.0; Odoo 8.0, 9.0, 10.0 and 11.0.

Completed Odoo Functional and Technical Training.

Functional Areas:

  • CRM/Sales

  • Inventory

  • Manufacturing

  • Accounting

  • Purchasing

Major Skills:

  • Business Process Reengineering

  • Efficiency Consulting

  • User Needs and GAP Analysis;

  • Functional and Technical Design;

  • Prototyping and Proof of Concepts;

  • Requirements Specifications;

  • Agile Implementation;

  • Data Migration;

  • Configuration & Customization (UI and code);

  • Integration - Odoo and non Odoo Applications and Services;

  • Training and Knowledge Transfer;

  • Go Live Support;

  • Helpdesk;

  • Version Upgrades and Migration.

  • Accounting Expert.

I have over 20 years of experience empowering and enabling users with enterprise information systems that make a real and measurable difference in their ability to proactively manage their businesses and organizations. 

I am a skilled Communicator and I only take the serious things seriously!

Ray Carnes
On 2/8/18, 3:16 PM

To expand on David's answer, I needed to add the Customer as a follower on a LEAD in v10.

I created an Automated Action that looked for crm.lead records created or updated that also had either a Customer or Email populated.

If the record had a Customer, I needed to just add that Customer as a follower (as long as it wasn't already).

If the record had an Email, I needed to check if a Customer already existed, and if not - create them.

This is the code I used for the Server Action:

if record.partner_id: 
partner = record.partner_id
else:
partner = env['res.partner'].search([('email','=',record.email_from)])
if not partner:
reg = {
'name': record.contact_name or record.email_from,
'email': record.email_from,
'type': 'contact',
}
partner = env['res.partner'].create(reg)
partner_id = partner.id
reg = {
'res_id': record.id,
'res_model': 'crm.lead',
'partner_id': partner_id,
}
if not env['mail.followers'].search([('res_id','=',record.id),('res_model','=','crm.lead'),('partner_id','=',partner_id)]):
follower_id = env['mail.followers'].create(reg)
0
David Todd
On 8/4/17, 11:12 AM

You've probably either already found your answer, or don't need it anymore, but I found myself having the same need to automatically add specific followers on my module written for Odoo v9. Here's my approach for the future people who have a similar question.

Preface: In my module, we do use the mail.message widget on our forms, but we use it as an automatic change tracker for auditing purposes. This allows us to see every change that was made to a form between saves, and which user made these changes. In the module I'm currently making, we want to be able to add specific people as followers when the form is in a specific stage.

First things first, we need a list of users that will become followers depending on the stage. Rather than hardcode them (we have a high turnover rate and it would introduce unnecessary updates), I'm creating a model called stage_followers that only users of the admin group can create/write/unlink. Normal users have only read access to that model.

First my model.py. On your actual form model, make sure you also have a stage selection field that is exactly the same. This will be used later in the write override

from openerp import models, fields
class stage_followers(models.Model):
_name = 'mymodule.stage_followers'
user = fields.Many2one('res.partner', required=True, domain=[('parent_id.name', '=', 'MyCompany')], string='User')
stage = fields.Selection(selection=yourStages, default='initial', string='Stage')

Next, my views.xml

<record model="ir.ui.view" id="mymodule.settings_sfollowers_form">
<field name="name">mymodule.settings_sfollowers.form</field>
<field name="model">mymodule.stage_followers</field>
<field name="arch" type="xml">
<form>
<field name="user" />
<field name="stage" />
</form>
</field>
</record>
<record model="ir.ui.view" id="mymodule.settings_sfollowers_list">
<field name="name">mymodule.settings_sfollowers.list</field>
<field name="model">mymodule.stage_followers</field>
<field name="arch" type="xml">
<tree>
<field name="user" />
<field name="stage" />
</tree>
</field>
</record>
<record model="ir.actions.act_window" id="mymodule.action_view_sfollowers_settings">
<field name="name">MyModule Settings - Stage Followers</field>
<field name="res_model">mymodule.stage_followers</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Stage Followers" groups="mymodule.group_mymodule_admin" id="mymodule.menu_settings_sfollowers" parent="mymodule.menu_settings" action="mymodule.action_view_sfollowers_settings" />

Whew, now that's out of the way, we can get into the nitty gritty. I wrote a method that lives inside the write override, it looks something like this.

def add_follower_id(self, res_id, model, partner_id):
follower_id = False
reg = {
'res_id': res_id,
'res_model': model,
'partner_id': partner_id
}
try:
follower_id = self.env['mail.followers'].create(reg)
except:
# This partner is already following this record
return False
return follower_id

The above should be fairly self explanatory. I've also written a method that will remove followers from specific records

def remove_follower_id(self, res_id, model, partner_id):
env = self.env['mail.followers']
domain = [('partner_id', '=', partner_id), ('res_id', '=', res_id), ('res_model', '=', model)]
try:
env.search(domain).unlink()
return True
except:
# The record was either not found, or the unlink operation was not allowed by the current user
return False

Now I mentioned a write override, I'll provide a small sample of what mine looks like. This is on whatever form you want to add the followers to.

@api.multi
def write(self, vals):
res = super(mymodule, self).write(vals) # Save the form
stage_followers = self.env['mymodule.stage_followers'].search([('stage', '=', vals['state'])])
for i in stage_followers:
add_follower_id(self, self.id, 'mymodule.myform', i['user'])
# Message posting is optional. Add_follower_id will still make the partner follow the record
messages = "Whatever you want to put in the message box."
if messages:
self.message_post(body=messages, partner_ids=self.message_follower_ids)
return res

Removing followers is the same as adding them above, except you might want to do the inverse of the search ( "!=" instead of "=" )

And that's basically it. Tweak this to your needs and it should do what you need. It sure does what I needed it to do.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 12/17/13, 5:27 PM
Seen: 5768 times
Last updated: 2/8/18, 3:16 PM