Posts

Magento salesforce integration with Customers in 3 Creative Steps

If you are running eCommerce website then you always fear to lose your customer accounts one solution is to keep the backup on daily basis but it comes very headache to keep lots of backups also need lots of space. Another solution is Magento 2 customers Salesforce integration.There are two ways for Magento Salesforce Integration for customers. One is to do is manually or another is to do it automatically (Using crone on specific events). We have described the automatically approach to synchronize Magento Salesforce Integration for customers.

Magento Salesforce Integration for Customers

Following are the steps to Magento Salesforce Integration for customers  based upon Magneto 2 customers creation or update

Step 1: Entering Observer event in the XML

This is the important part to handle the event into Magento 2. Go to your module ->etc->frontend and verify that the XML file named as the event.xml exists or not if exist then create. Open event.xml and do the following code inside this

 

    ............
    ............
    <event name="customer_save_after">
        <observer name="customer_save_after" instance="Package\Module Name\Observer\CustomerUpdateObserver" />
    </event>
    ............
    ............

Step 2: Creating Observer

Now we have to create the observer file to perform Magento Salesforce Integration operations according to our need. In my case my observer name is CustomerUpdateObserver.php . To create the file go to path project/app/code/package/module/Observer/ and create you observer here with following code.

add your needed helper files inside the constructor

 ........
 ......
 public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager, //to performs some operations 
        \Ipragmatech\Msintegration\Helper\Account\Data $helper //we have written our functions inside this helper
    ) {
        $this->_objectManager = $objectManager;
        $this->_helper = $helper;
    }

Add the following line of code inside the execute function of observer

       try {
            $customer = $observer->getEvent()->getCustomer();
            $salesforceId = $this->_helper->getClientSfId($customer->getId());
            if (empty($salesforceId)) {
                $id = $this->_helper->createAccount($customer);
                if (!empty($observer->getCustomerAddress())) {
                   $this->_helper->updateAddress($observer->getCustomerAddress(), $id);
                }
            } else {
                $this->_helper->updateAccount($salesforceId, $customer);
                if (!empty($observer->getCustomerAddress())) {
                    $this->_helper->updateAddress($observer->getCustomerAddress(), $id);

                }
            }
        } catch (\Exception $e) {
            // do what ever you wanna perform based on your exception log or some operation
        }


 

Step 3: Create Helper file

Go to helper folder create a folder named Account and inside folder create a file named Data.php. Inside the file, we need to write our functions for creating a customer account, updating customer, and updating customer address.

  • Creating customer account on the Salesforce
     public function createAccount($customer)
        {
            $name = $customer->getFirstname() . " " . $customer->getLastname();
            $email = $customer->getEmail();
            $lastUrl = "sobjects/Account/";
    
            $params = array(
                array(
                    "Name" => $name,
                )
    
            );
            $acountResult = $this->_helper->postAction($params, $lastUrl);
            $response = $acountResult ['response'];
            $status = $acountResult ['status'];
            $id = $response["id"];
            $mappingArray = [
                'customer_id' => $customer->getId(),
                'customer_salesforce_id' => $id,
    
            ];
            try {
                $model = $this->_objectManager->create('Ipragmatech\Msintegration\Model\Customermapping');
                $model->setData($mappingArray);
                $model->save();
            } catch (\Exception $e) {
                // put your own code
            }
    
            return $id;
        }
  • Updating customer account on Salesforce
     public function updateAccount($salesforceId, $customer)
        {
    
            $name = $customer->getFirstname() . " " . $customer->getLastname();
            $email = $customer->getEmail();
            $lastUrl = "sobjects/Account/$salesforceId";
            $params = array("Name" => $name);
            $updateAccount = $this->_helper->updateAction($params, $lastUrl);
            $status = $updateAccount ['status'];
    
            if ($status != 204) {
               // failed with status 
            }
            return true;
    
        }
  • Updating customer address on the Salesforce
        public function updateAddress($addressObject, $salesforceId)
        {
            $name = $addressObject->getFirstname() . " " . $addressObject->getLastname();
            $streets = $addressObject->getStreet();
            $country = $addressObject->getCountry();
            $city = $addressObject->getCity();
            $postCode = $addressObject->getPostCode();
            $company = $addressObject->getCompany();
            $state = $addressObject->getState();
            $telephone = $addressObject->getTelePhone();
            $strt = "";
            foreach ($streets as $street) {
                $strt .= "," . $street;
            }
    
            $lastUrl = "sobjects/Account/$salesforceId";
    
            $params = array(
                array(
                    "Name" => $name,
                    "BillingCity" => $city,
                    "BillingStreet" => $strt,
                    "BillingState" => $state,
                    "BillingPostalCode" => $postCode,
                    "BillingCountry" => $country,
                    "ShippingCity" => $city,
                    "ShippingStreet" => $strt,
                    "ShippingState" => $state,
                    "ShippingPostalCode" => $postCode,
                    "ShippingCountry" => $country,
                    "Phone" => $telephone
                )
            );
            try {
                $updateAddress = $this->_helper->updateAction($params, $lastUrl);
                $status = $updateAddress ['status'];
                if ($status != 204) {
                    //your own message call has been failed
                return true;
            } catch (\Exception $e) {
               //handle exception accordingly
            }
    
        }
    

Conclusion

The Magento Salesforce Integration for sync customers is an advantageous integration tool for Magento 2 and Salesforce. In this Magento Salesforce Integration you do not encounter any technical difficulties or confusion with it, support for this Magento Salesforce Integration is available, you can ask the question on this blog or you may have a look at a solution which we have already implemented for Magento Salesforce Integration, products and orders to Salesforce on their update/ create in Magento 2. Click here to know about our plugin. So don’t wait any longer, go for it!

References

Further Reading