Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: Multi Company Base

Entity: multi.company.abstract

Compute Method: _compute_company_id

View 17.0 View 19.0
Inputs Method Outputs
  • company_ids
  • company_ids.ids
for record in self:
            # Set this priority computing the company (if included in the allowed ones)
            # for avoiding multi company incompatibility errors:
            # - If this call is done from method _check_company, the company of the
            #   record to be compared.
-             # - Otherwise, current company of the user.
-             company_id = (
-                 self.env.context.get("_check_company_source_id")
-                 or self.env.context.get("force_company")
-                 or self.env.company.id
-             )
+             # - Otherwise, use current companies of the user, prioritizing main company.
+             # - As last resource, use the first allowed company.
+             company_id = self.env.context.get(
+                 "_check_company_source_id"
+             ) or self.env.context.get("force_company")
            if company_id in record.company_ids.ids:
                record.company_id = company_id
            else:
-                 record.company_id = record.company_ids[:1].id
+                 common_companies = self.env.companies & record.company_ids
+                 # Prioritize main company
+                 if common_companies and (self.env.company in common_companies):
+                     record.company_id = self.env.company.id
+                 # Or use the first common company
+                 elif common_companies:
+                     record.company_id = common_companies[0].id
+                 else:  # Use the fallback as last resource
+                     company = record.company_ids[:1]
+                     if company and isinstance(company.id, NewId):
+                         record.company_id = company._origin.id or False
+                     else:
+                         record.company_id = company.id
  • company_id

Back to entity