MuleSoft

Mastering the Salesforce Upsert Operation: A Comprehensive Guide

Pre-requisites

  • We need to have Salesforce developer account.
  • Create a Salesforce custom object with some fields, as per the requirement.
  • Click on create in the home section, which is on the right-hand side. Then click on custom object, then fill-in the name of the custom object. Then click on fields and relationship, then select the fields with the respective type of information.
  • In this scenario, we’ll be having 4 fields i.e. Amount__c, orderNo__c, product__c, Name__c and all the fields are having text as the type of information stored in it.
  • We need to keep the security token handy for that, click on profile and then click on setting which is on the right-hand top corner of the screen and type security token in the quick find section, and click on reset security token.
  • The security token will be sent to the registered mailId.

Upsert Operation in Salesforce

  • Upsert is the combination of two different operation i.e. Insert and Update.
  • Salesforce is a CRM (Customer Relationship Management), which is used to manage and analyze interactions with current and potential customers.
  • Upsert is the component provided by the Salesforce module to insert the record if the record does not exist or update the record if the record is already present by making use of externalId.
  • Why there is a need for upsert operation? As upsert eliminates the need for a two-step process by allowing to insert a record if it doesn’t exist or update it if it does, all in one go.
  • In this blog, we will see how to perform Salesforce upsert operation.
  • The requirement is we need to synchronize the data that is present in the local disk to the Salesforce custom object.

Steps:

1.To begin, a new Mule project needs to be created. Then, a file component should be dragged and dropped into the project for the On New or Updated File. Configure the directory, set the matcher to edit inline, and specify the filename as name_of_file.csv. to trigger the flow.

    2.Following the addition of the on new or updated file component, include two loggers to mark the beginning and the end of the flow. By logging the message “Start of flow ++ (flow.name as String)”, in expression mode.

    3.Include a logger to log the payload, as MuleSoft encourages extensive logger usage.

    4.Following the logger addition, introduce a transform message component to convert the payload that is in CSV format to JSON format.

    5.Following the transform message, designate one of the fields as the externalId in the Salesforce custom object. In this instance, orderNo will serve as the externalId, effectively acting as a primary key.

    6.Following the designation of the externalId, utilize another transform message to map the fields of the Salesforce custom object. In this scenario, the map function is employed, which accepts an array as input, iterates over it, and returns it in array format.

    7.Afterwards, upsert component will be added and configured as below.

    8.Now we’ll configure the object type which will we created in Salesforce and externalId, which is customer_Record__c and orderNo in my case.

    9.After configuring the object type and externalId, a transform message will be added following the upsert component. This transformation is not necessary but it will convert the response from Salesforce, which is in HashMap format (Java format to JSON format).

    10.Include a logger for the response from Salesforce. Upon completion, the flow will be structured as follows:

      11.Next, the application needs to be deployed, and the CSV file should be placed into the dedicated folder configured in the on new or updated file component. Once the application is up and running, the response will look like this.

        12.We will verify it using the Salesforce workbench.

          Author

          Aniket Pal

          Leave a comment

          Your email address will not be published. Required fields are marked *