0

I created a custom module where you can link multiple contacts in a many2many field and I want to automatically add them all as a follower. I've read this article:

Automated Action to Add Users as Followers Odoo 12

Adding followers works fine, but only when the fields have ONE user or partner in it. In my installation these are many2many fields. When I add more then one user or partner in one of the fields, the code crashes.

Do I need to change something in this code when I want to add more then 1 contact or user in one of the fields?

I have this python code in my automated action:

record.message_subscribe(partner_ids=[record.field1.id, record.field2.partner_id.id, record.field3.partner_id.id])

field1 = partner

field2 & field3 = user

Thanks for your help!

arryph
  • 2,725
  • 10
  • 15
jester
  • 1

1 Answers1

0

You can not use .id to get id of records when there is multiple record, you have to use .ids which returns a list of record ids. So you code will have to changed into:

field1_ids = record.field1.ids
field2_ids = record.field2.mapped('partner_id').ids
field3_ids = record.field3.mapped('partner_id').ids
record.message_subscribe(partner_ids=field1_ids+field2_ids+field3_ids)

So the main issue with your code was, you were trying to access field where the record could be multiple (as many2many), but in Odoo ORM you can not do that, it will always give Expected Singletone error for multiple records.

arryph
  • 2,725
  • 10
  • 15