5

The following record rule is defined in product module

<data noupdate="1">
    <record id="product_comp_rule" model="ir.rule">
        <field name="name" >Product multi-company</field>
        <field name="model_id" ref="model_product_template"/>
        <field name="global" eval="True"/>
        <field name="domain_force"> ['|',('company_id','=',user.company_id.id),('company_id','=',False)]</field>
    </record>
</data>

I want to edit it in my custom module to be

<record id="product.product_comp_rule" model="ir.rule">
    <field name="name" >All Products (Parent Company)</field>
    <field name="model_id" ref="product.model_product_template"/>
    <field name="global" eval="True"/>
    <field name="domain_force">['|','|',('company_id','=',user.company_id.id),('company_id','=','False'),('company_id','child_of',[user.company_id.id])] </field>
</record>
Charif DZ
  • 14,415
  • 3
  • 21
  • 40
Mustafa
  • 51
  • 1
  • 4

3 Answers3

10

You can use :

<function name="fix_er_role" model="ir.rule"/>

After that add the method to ir.rule and fix your data

class IRRule(models.Model):
    _inherit = 'ir.rule'
    
    def fix_er_role(self):
         rol_id = self.env.ref('product.product_comp_rule')
         rol_id = self.env['ir.rule'].search([('id','=',rol_id)])
         rol_id.write({'domain_force':['|','|',('company_id','=',user.company_id.id),('company_id','=','False'),('company_id','child_of',[user.company_id.id])]})

Edit : an other good solution for this requirement.

    <function name="write" model="ir.model.data">
         <!-- get the record if from data base -->
        <function name="search" model="ir.model.data">
            <value
              eval="[('module', '=', 'product'), ('name', '=', 'product_comp_rule')]"
              />
        </function>
       <!-- remove noupdate -->
        <value eval="{'noupdate': False}" />
    </function>


    <record id="product.product_comp_rule" model="ir.rule">
        <field name="name" >All Products (Parent Company)</field>
        <field name="model_id" ref="product.model_product_template"/>
        <field name="global" eval="True"/>
        <field name="domain_force">['|','|',('company_id','=',user.company_id.id),('company_id','=','False'),('company_id','child_of',[user.company_id.id])] </field>
    </record>

    <!-- reset noupdate -->
    <function name="write" model="ir.model.data">
        <function name="search" model="ir.model.data">
            <value
              eval="[('module', '=', 'product'), ('name', '=', 'product_comp_rule')]"
              />
        </function>
        <value eval="{'noupdate': True}" />
    </function>
dcg
  • 4,187
  • 1
  • 18
  • 32
Charif DZ
  • 14,415
  • 3
  • 21
  • 40
3

In Odoo 13 the XML solution worked just without the comments

    <function name="write" model="ir.model.data">
        <function name="search" model="ir.model.data">
            <value eval="[('module', '=', 'product'), ('name', '=', 'product_comp_rule')]"/>
        </function>
        <value eval="{'noupdate': False}" />
    </function>

    <record id="product.product_comp_rule" model="ir.rule">
        <field name="name" >All Products (Parent Company)</field>
        <field name="model_id" ref="product.model_product_template"/>
        <field name="global" eval="True"/>
        <field name="domain_force">['|','|',('company_id','=',user.company_id.id),('company_id','=','False'),('company_id','child_of',[user.company_id.id])] </field>
    </record>

    <!-- reset noupdate -->
    <function name="write" model="ir.model.data">
        <function name="search" model="ir.model.data">
            <value eval="[('module', '=', 'product'), ('name', '=', 'product_comp_rule')]"/>
        </function>
        <value eval="{'noupdate': True}" />
    </function>

The issue was already reported by someone else https://github.com/odoo/odoo/issues/45592

Lenormju
  • 4,078
  • 2
  • 8
  • 22
0

We can do it in another sample way using noupdate="0"

<data noupdate="0">
    <record id="product.product_comp_rule" model="ir.rule">
        <field name="name" >All Products (Parent Company)</field>
        <field name="model_id" ref="product.model_product_template"/>
        <field name="global" eval="True"/>
        <field name="domain_force">['|','|',('company_id','=',user.company_id.id),('company_id','=','False'),('company_id','child_of',[user.company_id.id])] </field>
    </record>
</data>
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
  • 1
    The solution isn't working from odoo 8 onwards, hence you need first to disable the update for that specific record and write your changes, then preferably put back the noupdate = True – rachid el kedmiri Mar 04 '21 at 22:27