Magento – Customer export not working after creating custom attributes in magento 2.2.5

admincustomer-gridexportmagento2uicomponent

I have created a custom attribute named "Approve Account" while export getting below error.

Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Framework\View\Element\UiComponentFactory::argumentsResolver() must be of the type array, null given, called in /var/www/html/tgbl222/vendor/magento/framework/View/Element/UiComponentFactory.php on line 220 and defined in /var/www/html/tgbl222/vendor/magento/framework/View/Element/UiComponentFactory.php:172 Stack trace: #0 /var/www/html/tgbl222/vendor/magento/framework/View/Element/UiComponentFactory.php(220): Magento\Framework\View\Element\UiComponentFactory->argumentsResolver('approve_account', NULL) #1 /var/www/html/tgbl222/vendor/magento/module-ui/Component/Listing/Columns/Column.php(77): Magento\Framework\View\Element\UiComponentFactory->create('approve_account', 'int', Array) #2 /var/www/html/tgbl222/vendor/magento/module-ui/Component/MassAction/Filter.php(184): Magento\Ui\Component\Listing\Columns\Column->prepare() #3 /var/www/html/tgbl222/generated/code/Magento/Ui/Component/MassAction/Filter/Interceptor.php(63): Magento\Ui\Component\MassAction\Filter->p in /var/www/html/tgbl222/vendor/magento/framework/View/Element/UiComponentFactory.php on line 172

/app/code/Namespace/Module/view/adminhtml/ui_component/customer_listing.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="customer_columns">
    <column name="approve_account">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Magento\Config\Model\Config\Source\Yesno</item>
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">select</item>
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                <item name="dataType" xsi:type="string">select</item>
                <item name="label" xsi:type="string" translate="true">Account Active</item>
                <item name="sortOrder" xsi:type="number">280</item>
            </item>
        </argument>
    </column>
</columns>

/app/code/Namespace/Module/setup/installData.php

/**
 * {@inheritdoc}
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();

    /**
     *  Customer attributes
     */
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();

    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);


    /**
     * Create customer attribute account_approve
     */
    $customerSetup->addAttribute(Customer::ENTITY, self::APPROVE_ACCOUNT,
        [
            'type' => 'int',
            'label' => 'Approve Account',
            'input' => 'select',
            "source"   => "RBC\Customersegment\Model\Config\Source\CustomerYesNoOptions",
            'required' => false,
            'default' => '1',
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 215,
            'position' => 215,
            'system' => false,
        ]);
    $approve_account = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, self::APPROVE_ACCOUNT)
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer','customer_account_create','customer_account_edit'],
        ]);
    $approve_account->save();

    $setup->endSetup();
}

Best Answer

I have tried the below code with static datatype but it has consequences as mentioned below:

$customerSetup->addAttribute(Customer::ENTITY, self::APPROVE_ACCOUNT,
        [
            'type' => 'static',
            'label' => 'Approve Account',
            'input' => 'select',
            "source"   => "RBC\Customersegment\Model\Config\Source\CustomerYesNoOptions",
            'required' => false,
            'default' => '1',
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 215,
            'position' => 215,
            'system' => false,
        ]);

Using static datatype in attribute will solve the problem in grid export but will throw error while reindexing as the the attribute type is static, it will look into customer_entity table for this field which you might not have created. So in your case, you can follow the suggestions as mentioned below.

It seems that you have not created field in customer_entity table, you are using attribute here, so make sure you have not added field in customer_listing layout in ui_component directory. Also you don't have to add the field in indexer.xml as for attributes, it automatically updates data in customer_grid_flat table.

I hope this helps you with your problem.

Related Topic