1

I've try this:

[('company_id','=',company_id)]

self.env.company_id

self.env.user.company_id

But those all 3 ways are leaded to default company, but not current company.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
fudu
  • 617
  • 1
  • 10
  • 19
  • Check in `self.env.context` – Muhammad Yusuf Apr 26 '22 at 09:46
  • @MuhammadYusuf sir, is there anyway to call `self.env.context` inside of domain of record rules? – fudu Apr 26 '22 at 10:00
  • Yes, try something like this `[('company_id','in',context.get('allowed_company_ids'))]` OR `[('company_id', '=', context.get('company_id'))]` – Muhammad Yusuf Apr 26 '22 at 10:23
  • @MuhammadYusuf not working sir, it say `ValueError: : "name 'context' is not defined" while evaluating "[('company_id','in',context.get('allowed_company_ids'))]"` – fudu Apr 26 '22 at 10:36
  • Sorry in record rules there will not be context will be where the is an active model in ir.rule you can use ` [('company_id','=',[user.company_id.id])] ` – Muhammad Yusuf Apr 26 '22 at 10:39
  • @MuhammadYusuf so we have to use python code to handle this? – fudu Apr 26 '22 at 10:40

5 Answers5

0

I suggest creating a related field that points to the current company or just creating a new stored computed field show_field_XY. In the _compute method you can access the context like this:

self.env.context.get('company_id')

Finally, just access that field in the domain and you should be good to go.

Josip Domazet
  • 2,246
  • 2
  • 14
  • 37
  • thanks you for comment, i'll try and reply asap – fudu May 04 '22 at 03:51
  • sir, i've try your solution but with domain of record rule such as `['|',('company_id','=',company_id),('company_id','=',False)]`, how do you call that new field? for example, we defined that field is "current_company", how to call it? we can't set `('company_id','=','current_company')` – fudu May 09 '22 at 03:50
  • You need to remove the quotes: `('company_id','=',current_company)`. Also add the field itself with `` (you can make it invisible if you want to). Also make sure the field is computed if still does not work. – Josip Domazet May 09 '22 at 12:38
0

try this

current_company = self.env.user.company_id
Shilal
  • 46
  • 1
0

As i check, if we use context to get current company, and in case we using multi company, we can only get the first company.

In that case, we should use company_ids to get all the companies of user instead.

Example:

['|',('company_id','in',company_ids),('company_id','=',False)]

Hope it's help.

fudu
  • 617
  • 1
  • 10
  • 19
0

Have you tried self.env.company ? You could use a separate field to determine this and use that in your xml..

active_company_id = fields.Many2one(
        "res.company",
        string="Company",
        default=lambda self: self.env.company,
    )
NinjaBat
  • 370
  • 4
  • 20
0

You should create record rules for your module like the following, and make sure in your class model you have the company_id set up already:

<!-- Rules --><record id="yourmodullid_comp_rule" model="ir.rule"><field name="name">""" multi-company</field><field name="model_id" ref="model_yourmodel" /><field name="domain_force">['|',('company_id', 'in', company_ids),('company_id','=',False)]</field></record><!-- ___________ -->
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77