This question has been flagged
8 Replies
32636 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?

Avatar
Discard

Write to the message_follower_ids field.

Author

Please, can you explain more!

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

Author

it returns false

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

Author

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

Best Answer

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)
Avatar
Discard
Best Answer

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.

Avatar
Discard