I want to update customer information programmatically. so I have used below code.
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
try{
$path = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('catalog/import/epicore/UPB-customers.csv');
$dataExcel = fopen($path, "r");
while (!feof($dataExcel) ) {
$line_of_text[] = fgetcsv($dataExcel, 1024);
}
array_shift($line_of_text);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
foreach($line_of_text as $customerNewData){
$websiteId = $this->storeManager->getWebsite()->getWebsiteId();
$customer = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface')->getById($customerNewData[0]);
//echo "<pre>"; print_r($customer->getData()); exit;
if(count($customer)){
$data = array();
$email = $customer->getEmail();
$customer->setWebsiteId($websiteId);
$customer->setData("custid", $customerNewData[1]);
$customer->setData("custnum", $customerNewData[2]);
$customer->setData("salesforceid", $customerNewData[3]);
$customer->setEmail($email);
$customer->save();
}else{
}
}
} catch(\Exception $e) {
echo $e->getMessage(); exit;
}
$this->_redirect('*/*/');
return;
}
But Its returns "Please enter email address" error.