Skip to content

How to create company programmatically magento 2 b2b?

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('MagentoCompanyApiCompanyRepositoryInterface');    


$companyObj = $obj->create('MagentoCompanyApiDataCompanyInterface');

$dataObj = $obj->create('MagentoFrameworkApiDataObjectHelper');

$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,
        MagentoCompanyApiDataCompanyInterface::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): MagentoCompanyModelSaveValidatorPool->execute(Object(MagentoCompanyModelCompanyInterceptor), Object(MagentoCompanyModelCompanyInterceptor))

Thanks in advance!