Skip to Content
Menu
This question has been flagged
2 Replies
8021 Views

Hi,

I created a custom model. Currently, all records created in a different company is visible to all companies. I need to see for example the records created in company A should only visible in company A and records created in company B should only visible to company B. How can I achieve this.

Avatar
Discard
Best Answer

Hi,

If you don't have a company field in your custom model then you have to add one like below

company_id = fields.Many2one('res.company', 'Company', required=True, index=True,
default=lambda self: self.env.company)

Then you have to write security rules regarding the visibility of the records

For example

<record model="ir.rule" id="sale_order_comp_rule">
<field name="name">Sales Order multi-company</field>
<field name="model_id" ref="model_sale_order"/>
<field name="global" eval="True"/>
<field name="domain_force">[('company_id', 'in', company_ids)]</field>
</record>

Above is the example of the model sale order. You can redefine with your custom model

You can also refer the following documentations

https://www.odoo.com/documentation/13.0/howtos/company.html

https://www.cybrosys.com/blog/how-to-handle-multi-company-odoo-custom-module

Regards

Avatar
Discard
Best Answer

Hi,

Add company field to the corresponding model and add records rule to the model. it is explained here in this blog: https://www.cybrosys.com/blog/how-to-handle-multi-company-odoo-custom-module

Add a field company_id into your model,

    company_id = fields.Many2one('res.company', string='Company',  default=lambda self: self.env.company)

And add a record rule for corresponding model like this,

<record model="ir.rule" id="record_not_shared_multi_company_rule">
    <field name="name">Non Shared Records:model</field>
    <field name="model_id" ref="module.model_model"/>
    <field name="global" eval="True"/>
    <field name="domain_force">
       [('company_id', 'in', company_ids)]
    </field>
</record>


Thanks

Avatar
Discard