I am using B2B extensions and I want to add custom attributes to company entity, As a text field and create 6-digit number randomly for that i'm using

$customcompanyattribute     = mt_rand(100000, 999999);

Attribute should be read-only and should generate value when a new company form is submitted.

I found that we don't have a mechanism like Magento has for adding attributes to customer CustomerSetupFactory, and using

Here is the work i did.


 <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="general">
        <field name="account_number" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                <item name="disabled" xsi:type="boolean">true</item>

                    <item name="source" xsi:type="string">Account Number</item>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                <label translate="true">Account Number</label>



namespace Custom\OverrideCompany\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallSchema implements InstallSchemaInterface {
    const COMPANY_TABLE_NAME = 'company';

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context){
        $table = $setup->getTable(self::COMPANY_TABLE_NAME);

        $columns = [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 11,
                    'nullable' => true,
                    'comment' => 'Account Number'
        $connection = $setup->getConnection();
        foreach ($columns as $col_name => $col_array){
            $connection->addColumn($table, $col_name, $col_array);



namespace Custom\OverrideCompany\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface

     * {@inheritdoc}
    public function upgrade(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $installer = $setup;

        if (version_compare($context->getVersion(), '1.0.0', '<')) {
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 11,
                    'nullable' => true,
                    'comment' => 'Account Number'

enter image description here How to achieve it.

  • did you find a solution to this? – Asad Ullah Jul 2 at 15:16

I am assuming that you have your custom column added in the database and custom field present on the admin company edit page in the General section.

  1. For reading the column value from the database into the field on the admin page:

Add in di.xml:

<!-- override core class in order to have the custom column show on the admin company edit page -->
<preference for="Magento\Company\Model\Company\DataProvider" type="MyVendor\MyModule\Model\Company\DataProvider" />

And in your DataProvider.php:

namespace MyVendor\MyModule\Model\Company;

class DataProvider extends \Magento\Company\Model\Company\DataProvider {
    public function getGeneralData(\Magento\Company\Api\Data\CompanyInterface $company) {
        $result = parent::getGeneralData($company);
        # add custom column value to the General data section so that the value populates the custom field on the admin company edit page
        $result['my_custom_column'] = $company->setMyCustomColumn();
        return $result;

  1. For saving the field into the database custom column:

Add in di.xml:

<!-- override core class by plugin in order to save value from the admin company edit page into the custom column my_custom_column -->
<type name="Magento\Company\Model\CompanyRepository">
    <plugin name="myvendor_mymodule_after_save_company" type="MyVendor\MyModule\Plugin\Model\CompanyRepository" />

And in your CompanyRepository.php:

namespace MyVendor\MyModule\Plugin\Model;

class CompanyRepository {
    public function afterSave(\Magento\Company\Model\CompanyRepository $companyRepository, \Magento\Company\Api\Data\CompanyInterface $company) {
        $my_custom_column = 'some value here';
        return $company;
| improve this answer | |
New contributor
user1765811 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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