Upsert Records In Salesforce

 

In this tutorial we will demonstrate how to Upsert Records In Salesforceusing Using Salesforce upsert operation.

Using the upsert operation, you can either insert or update an existing record in one call. To determine whether a record already exists, the upsert statement or Database method uses the record’s ID as the key to match records, a custom external ID field, or a standard field with the idLookup attribute set to true.

  • If the key is not matched, then a new object record is created.
  • If the key is matched once, then the existing object record is updated.
  • If the key is matched multiple times, then an error is generated and the object record is neither inserted or updated.

Ref: https://docs.mulesoft.com/connectors/salesforce/salesforce-connector-reference#upsert

Prepare the test data to upsert

[
   {
      "CUSTOMER_ID":"1",
      "CUSTOMER_NAME":"TEST1",
      "ADDRESS1":"Flat 101",
      "ADDRESS2":"Viman Nagar",
      "POSTALCODE":"201018",
      "CITY":"PUNE",
      "Phone":"8476539611"
   },
   {
      "CUSTOMER_ID":"2",
      "CUSTOMER_NAME":"TEST2",
      "ADDRESS1":"Flat 102",
      "ADDRESS2":"Viman Nagar 1",
      "POSTALCODE":"201019",
      "CITY":"PUNE",
      "Phone":"847653911"
   },
   {
      "CUSTOMER_ID":"3",
      "CUSTOMER_NAME":"TEST3",
      "ADDRESS1":"Flat 103",
      "ADDRESS2":"Viman Nagar",
      "POSTALCODE":"201018",
      "CITY":"PUNE",
      "Phone":"8476539611"
   },
   {
      "CUSTOMER_ID":"4",
      "CUSTOMER_NAME":"TEST4",
      "ADDRESS1":"Flat 104",
      "ADDRESS2":"Viman Nagar 4",
      "POSTALCODE":"201019",
      "CITY":"PUNE",
      "Phone":"847653911"
   },
   {
      "CUSTOMER_ID":"5",
      "CUSTOMER_NAME":"TEST5",
      "ADDRESS1":"Flat 104",
      "ADDRESS2":"Viman Nagar 4",
      "POSTALCODE":"201019",
      "CITY":"PUNE",
      "Phone":"847653911"
   }
]

 

Create a project in anypoint studio and configure the listener to trigger the request

Drag and drop upsert operation from mule palette and configure the connection detail

Upsert Records In Salesforce

 

Add the transformation

 

Update the transform message to upsert the data in salesforce

%dw 2.0
output application/java
---

payload map ((item,index) ->
{
    Name:item.CUSTOMER_NAME,
    Account_Number_c__c:item.CUSTOMER_ID,
    Phone:item.Phone
})

 

Provide the parameters under upsert operation

External id field name: Contains the name of the field on this object with the external ID field attribute for custom objects or the idLookup field property for standard objects.

Type: Type of record to be upserted.

Records: Records to be added to your organization.

Note: Please make sure external id field name is present, in our case we have created a custom field and provide as external id

 

 

We can see below Account_Number_c_c is configured as External id field name, on ths basis of this field it would be decided either the record will be update or create a new record

Upsert Records In Salesforce

 

Create the transform message to get the response

%dw 2.0
output application/json 
---
payload

 

Deploy the project and test from soapui/postman

Existing records are present in Salesforce

 

 

Test the project

Upsert Records In Salesforce

 

5 records are updated and 1 record is inserted in Salesforce, we can see response also for the created record it return the true value and for updated records it returns false value

 

 

Sample mule application: salesforceupsert sample application

SOAP UI application: SalesforceUpsert-soapui-project

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments