Monday , January 21 2019

DML Operation

DML Operation contd

Upserting records

This command incorporates two DML operation i.e. insert and update in one statement. It has to be provided with a unique identifier such as Record Id, so that it can search the database and find the requested record, if there is no record matching the record Id then a new record is created, if a match found then the record is updated. But if the record Id or key passed have multiple matches, then it neither updates nor creates a record and throw an error


Upsert Image


Upsert operation is supported in Dataloader, Apex, and API. It is feasible to create parent child relationship in one statement.


Below is the schematic representation of an Upsert Statement.



Upsert Schematic

Benefits of using Upsert:


It simplifies the process of insertion and updating the records, information about Salesforce Id is not mandatory we can use external Id for this purpose. We do not have to manually export and match the records.  It helps in preventing the duplication of records.


Best Practice for Upsert:


Make external Id, Unique and case insensitive. Make use of compound fields to make a unique identifiers.


Use Case for Upsert:

Export data from production to sandbox.

  1. Create a new field and make it as external Id, follow the best practice to make this field
  2. Deploy new field in the sandbox
  3. Start export of data from production and import to sandbox using the various tools available like dataloader or data import wizard
  4. Use the newly created field for matching the data in sandbox.


Upsert command and external id are very handy when we are integrating data from external datasource, with the help of external id we can reference our record. Most of the data uploading tool like dataloader, dataimport wizard or 3rd party connectore like boomi has options to specify the external Id.


Merge Records


If there are duplicates in your org then Merge operation comes into picture to help you clean up the data.


Merge operation can take up to three records , update the selected record and delete the other two records, in case the delete records have related objects then these objects get reparented.


Merge is mainly available on Contacts, lead and account object


Steps to merge these standard objects are mentioned below:


  1. Merge Duplicate Contacts
  2. Merge Duplicate Accounts
  3. Merge Duplicate Leads



About Sumit Datta

Sumit Datta
I am a 3x Certified Salesforce developer with overall 5 years of IT experience and 3 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

Check Also


How to create a Modal box in Lightning Component?

How to create a Modal box in Lightning Component? In this post we are going …

Salesforce Lightning Training

How to send parameters in AuraEnabled methods in Salesforce Lightning Components?

How to send parameters in AuraEnabled methods in Salesforce Lightning Components? Passing value to and …

Salesforce Lightning Tutorials

How to use lightning accordion

How to use lightning accordion and how to dynamically add accordion section in lightning. In …

Salesforce Lightning Tutorial

How to fetch and display list of records in Lightning Component

How to fetch and display list of records in Lightning Component? In this post we …

Leave a Reply

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