Odoo Help

2

add followers automatically

By
sayed fathy
on 12/17/13, 5:27 PM 4,834 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
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: 4834 times
Last updated: 8/4/17, 11:15 AM