Alterations to core:
One line uses $this->saveEntity($lead); instead of the core repo.

@param $companyId
@param $leadId

@return array

@throws \Exception
352193
public function setPrimaryCompany($companyId, $leadId)
    {
        $companyArray      = [];
        $oldPrimaryCompany = $newPrimaryCompany = false;

        $lead = $this->getEntity($leadId);

        $companyLeads = $this->companyModel->getCompanyLeadRepository()->getEntitiesByLead($lead);

        /** @var CompanyLead $companyLead */
        foreach ($companyLeads as $companyLead) {
            $company = $companyLead->getCompany();

            if ($companyLead) {
                if ($companyLead->getPrimary() && !$oldPrimaryCompany) {
                    $oldPrimaryCompany = $companyLead->getCompany()->getId();
                }
                if ($company->getId() === (int) $companyId) {
                    $companyLead->setPrimary(true);
                    $newPrimaryCompany = $companyId;
                    $lead->addUpdatedField('company', $company->getName());
                } else {
                    $companyLead->setPrimary(false);
                }
                $companyArray[] = $companyLead;
            }
        }

        if (!$newPrimaryCompany) {
            $latestCompany = $this->companyModel->getCompanyLeadRepository()->getLatestCompanyForLead($leadId);
            if (!empty($latestCompany)) {
                $lead->addUpdatedField('company', $latestCompany['companyname'])
                    ->setDateModified(new \DateTime());
            }
        }

        if (!empty($companyArray)) {
            // Alteration to core start.
            $this->getRepository()->saveEntity($lead);
            // Alteration to core end.
            $this->companyModel->getCompanyLeadRepository()->saveEntities($companyArray, false);
        }

        // Clear CompanyLead entities from Doctrine memory
        $this->em->clear(CompanyLead::class);

        return ['oldPrimary' => $oldPrimaryCompany, 'newPrimary' => $companyId];
    }
49848
public function validateLeadForPost(Lead $lead)
    {
        $invalidProperties = ['id', 'organization', 'tasks', 'opportunities'];
        foreach ($invalidProperties as $invalidProperty) {
            $getter = 'get' . ucfirst($invalidProperty);
            if ($lead->$getter()) {
                throw new InvalidNewLeadPropertyException('Cannot post ' . $invalidProperty . ' to new lead.');
            }
        }
    }
259151
public function getCompany($companyId, $fields = ['*'])
    {
        $apiUrl = $this->getEndpoint('companies');
        $apiUrl .= $companyId;

        $params = array(
            'fields' => implode(',', $fields)
        );

        $apiData = $this->apiGet($apiUrl, $params);

        return $this->decodeSingle($apiData);
    }
542924
public static List<CPInstance> findByUuid_C(String uuid, long companyId) {
		return getPersistence().findByUuid_C(uuid, companyId);
	}
298311
@Override
	public java.util.List<com.liferay.commerce.product.model.CProduct> getCProductsByUuidAndCompanyId(
		String uuid, long companyId) {
		return _cProductLocalService.getCProductsByUuidAndCompanyId(uuid,
			companyId);
	}
146069
private function getBusinessEntity($id)
    {
        //services
        $businessEntityManager = $this->get('victoire_core.helper.business_entity_helper');

        //get the businessEntity
        $businessEntity = $businessEntityManager->findById($id);

        //test the result
        if ($businessEntity === null) {
            throw new \Exception('The business entity ['.$id.'] was not found.');
        }

        return $businessEntity;
    }
33305
public function ofLead($leadId)
    {
        $response = giga_facebook_get($leadId . '/custom_labels');

        if (!isset($response->data) || !is_array($response->data)) {
            return false;
        }

        $channels = $response->data;

        $channelIds = [];
        foreach ($channels as $channel) {
            $channelIds[] = $channel->id;
        }

        return $channelIds;
    }
901871
public function addCompanyRelations(int $idProductList, array $companyIds): void
    {
        foreach ($companyIds as $idCompany) {
            $productListCompanyEntity = new SpyProductListCompany();
            $productListCompanyEntity->setFkProductList($idProductList)
                ->setFkCompany($idCompany)
                ->save();
        }
    }
472680
@Override
	public List<CPDefinition> getCPDefinitionsByUuidAndCompanyId(String uuid,
		long companyId) {
		return cpDefinitionPersistence.findByUuid_C(uuid, companyId);
	}
541839
public function mapToCompany(
        RestCompaniesRequestAttributesTransfer $restCompaniesRequestAttributesTransfer,
        CompanyTransfer $companyTransfer
    ): CompanyTransfer {
        if ($restCompaniesRequestAttributesTransfer->getExternalReference() !== null) {
            $companyTransfer->setExternalReference(
                $restCompaniesRequestAttributesTransfer->getExternalReference()
            );
        }

        if ($restCompaniesRequestAttributesTransfer->getName() !== null) {
            $companyTransfer->setName($restCompaniesRequestAttributesTransfer->getName());
        }

        return $companyTransfer;
    }
1081040
@Override
	public void removeByCompanyId(long companyId) {
		for (CPOptionValue cpOptionValue : findByCompanyId(companyId,
				QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
			remove(cpOptionValue);
		}
	}
699592
private function getExtendedFieldValuesMultiple(
        $extendedFieldList = [],
        $leadIds = []
    ) {
        /** @var EntityManager $em */
        $em = $this->getEntityManager();

        $leadIdsStr = "'".implode("','", $leadIds)."'";

        $selects = [];
        foreach (['string', 'float', 'boolean', 'date', 'datetime', 'time', 'text'] as $data_type) {
            foreach (['', '_secure'] as $secure) {
                $xrefTable = MAUTIC_TABLE_PREFIX."lead_fields_leads_{$data_type}{$secure}_xref";

                $selects[] = <<<EOSQL
SELECT lead_id, lead_field_id, value, '{$data_type}' AS data_type 
FROM  {$xrefTable}
WHERE lead_id IN ({$leadIdsStr})
EOSQL;
            }
        }
        $xrefQuery = implode("\nUNION\n", $selects);

        $leadsTable      = $em->getRepository('MauticLeadBundle:Lead')->getTableName();
        $lt              = $em->getRepository('MauticLeadBundle:Lead')->getTableAlias();
        $leadFieldsTable = $em->getRepository('MauticLeadBundle:LeadField')->getTableName();
        $lft             = $em->getRepository('MauticLeadBundle:LeadField')->getTableAlias();

        $where = "{$lft}.object IN ('extendedField','extendedFieldSecure') ";
        if (!empty($extendedFieldList)) {
            $ids             = array_map(
                function ($e) {
                    return $e['id'];
                },
                $extendedFieldList
            );
            $leadFiieldIdStr = "'".implode("','", $ids)."'";
            $where           = "{$lft}.id IN ({$leadFiieldIdStr}) ";
        }

        $sql = <<<EOSQL
SELECT x.lead_id, x.alias, v.value, v.data_type
FROM
(
    SELECT {$lt}.id AS lead_id,
    {$lft}.id as lead_field_id,
    {$lft}.alias
    FROM {$leadsTable} {$lt} CROSS JOIN {$leadFieldsTable} {$lft}
    WHERE {$where}
    AND {$lt}.id  IN ({$leadIdsStr})
) x
LEFT JOIN
(
    {$xrefQuery}
) v USING (lead_id, lead_field_id)
EOSQL;

        $query = $em->getConnection()->prepare($sql);
        $query->execute();
        $results = $query->fetchAll();

        // Group results by leadId.
        $leads = [];
        foreach ($results as $row => $result) {
            $leads[$result['lead_id']][$result['alias']] = $result['value'];
            unset($results[$row]);
        }

        return $leads;
    }
1286669
public static List<CommercePriceListUserSegmentEntryRel> findByUuid_C(
		String uuid, long companyId) {
		return getPersistence().findByUuid_C(uuid, companyId);
	}
218438
protected function createCompanyResponseTransfer(CompanyTransfer $companyTransfer): RestCompaniesResponseTransfer
    {
        $restCompaniesResponseAttributesTransfer = new RestCompaniesResponseAttributesTransfer();

        $restCompaniesResponseAttributesTransfer->fromArray(
            $companyTransfer->toArray(),
            true
        );

        $restCompaniesResponseTransfer = new RestCompaniesResponseTransfer();

        $restCompaniesResponseTransfer->setIsSuccess(true)
            ->setRestCompaniesResponseAttributes($restCompaniesResponseAttributesTransfer);

        return $restCompaniesResponseTransfer;
    }
1341180
@Override
	public List<CommerceAccount> filterFindByCompanyId(long companyId) {
		return filterFindByCompanyId(companyId, QueryUtil.ALL_POS,
			QueryUtil.ALL_POS, null);
	}
1098292
@Override
	public List<CommercePriceEntry> findByCompanyId(long companyId) {
		return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
			null);
	}
370356
public static List<CPDefinitionInventory> findByUuid_C(String uuid,
		long companyId) {
		return getPersistence().findByUuid_C(uuid, companyId);
	}