0

I want to create a company programmatically from a standalone script. Can someone please help me out with this. how can I create it.

Here is the code I am using to create company.

$companyRepo = $obj->create('Magento\Company\Api\CompanyRepositoryInterface');    


$companyObj = $obj->create('\Magento\Company\Api\Data\CompanyInterface');

$dataObj = $obj->create('\Magento\Framework\Api\DataObjectHelper');

$data = [
    'company_name' => [
        'Test Company'
    ],
    'company_email' => '[email protected]',
    'status' => '1',
    'street' => ['test','test2'],
    'city' => 'test city',
    'postcode' => '12345',
    'country_id' => 'US',
    'firstname' => 'test f',
    'lastname' => 'test l',
    'email' => '[email protected]',
    'customer_group_id' => 1,
    'website_id' => 1
];

$dataObj->populateWithArray(
        $companyObj,
        $data,
        \Magento\Company\Api\Data\CompanyInterface::class
    );


$companyRepo->save($companyObj);

But it is throwing below error.

EXCEPTION::#0 /var/www/html/project/vendor/magento/module-company/Model/Company/Save.php(79): Magento\Company\Model\SaveValidatorPool->execute(Object(Magento\Company\Model\Company\Interceptor), Object(Magento\Company\Model\Company\Interceptor))

Thanks in advance!

6
  • What have you tried, and why is it not working? Aug 26, 2020 at 6:04
  • @simonthesorcerer I have created standalone script and called company repository save method by passing company interface but somehow its throwing below exeception. EXCEPTION::#0 /var/www/html/project/vendor/magento/module-company/Model/Company/Save.php(79): Magento\Company\Model\SaveValidatorPool->execute(Object(Magento\Company\Model\Company\Interceptor), Object(Magento\Company\Model\Company\Interceptor)) #1
    – Deep Joshi
    Aug 26, 2020 at 6:06
  • It helps if you add your code and error messages to the question. Aug 26, 2020 at 6:07
  • @simonthesorcerer have added the code which I am using please refer the edited description. Thanks!
    – Deep Joshi
    Aug 26, 2020 at 6:11
  • Hm, is there no exception message? The code looks good to me. You could try with Magento\Company\Model\CompanyFactory::create instead of using the data helper. Or debugging SaveValidatorPool what exactly is wrong there. Aug 26, 2020 at 6:14

1 Answer 1

0

You need to pass the correct array and correct details in the company repo. then It'll be work.

So company creation you need to follow the process.

  1. You need to create a customer that will be assigned to the company.
  2. Using the customer id you create and assign the company on the admin side.
  3. Customer id/super_user_id(company admin), group id is mandatory to create a company programmatically.

Below my the working code that I have used to create a company via the import feature in the admin.

\Magento\Company\Api\CompanyRepositoryInterface $companyRepository,
\Magento\Company\Api\Data\CompanyInterface $companyInterface,
\Magento\Framework\Api\DataObjectHelper $objectHelper

$this->companyRepository = $companyRepository;
$this->companyInterface = $companyInterface;
$this->objectHelper = $objectHelper;

$companyRepo = $this->companyRepository;
$companyObj = $this->companyInterface;
$dataObj = $this->objectHelper;

 $company = [
               "company_name" => "Ravi Company",
               "company_email" => "[email protected]",
               "street" => ["100 Big Tree Avenue"],
               "city" => "San Francisco",
               "country_id" => "US",
               "region" => "CA",
               "region_id" => "12",
               "postcode" => "99999",
               "telephone" => "4155551212",
               "super_user_id" => 5,
               "customer_group_id" => 1
           ];

$dataObj->populateWithArray(
     $companyObj,
     $company,
     \Magento\Company\Api\Data\CompanyInterface::class
);

$companyRepo->save($companyObj);

Make sure before that you can create customer-first that you need to assign to company and that customer id pass in "super_user_id" parmenter in company.

If any amends in this, please provide your suggestion

Thanks, Ravi Chomal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.