DML Operations In Salesforce

Introduction to Salesforce DML 

The full form of DML is Data Manipulation Language and through DML Statements we can implement DML operations in the salesforce. DML is essential in Apex, as every business requires some changes and modifications regularly. Database Methods are the system methods that are also used to carry out DML operations. Database methods are more flexible than DML statements. The data manipulation operations like insertion, deletion, and updation are supported in Salesforce. The DML operations are implemented in the following way:

Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Training 

Salesforce DML Operations

Insert Operation

The DML insert operation is used to establish new data in the database. The data is added for any custom or standard object.


Student newStud= new Student(Stud_Name=’Rakesh’);

Try {

Insert newStud;

} catch(DmlException d) {

   Exception Code


 The above code tells us how to insert a new student name. The try-and-catch block is used to check whether a student with name Rakesh is already present in the database or not.

Checkout SalesForce Tutorial

Delete Operation

The delete operation is used to delete the existing records of a standard object or a custom object. The records can be anything like Student details, employee details, etc. An example for this deletion operation is as follows:

Student[]  OldStuds= [SELECT Stud_ID , Stud_Name FROM Student WHERE Stud_Fee<10000];


Delete OldStuds;

} catch (DmlException d) {

   Exception Code


The above code deletes the records of the students who have fees lesser than 10000.

 MindMajix YouTube Channel

Update Operation

This DML operation updates or modifies the existing records of a standard object or a custom object. The following example shows how we can update an existing record:

Student s = new Student(Name=” Rakesh”)


Student myStud = [ SELECT Stud_ID, Stud_Name, Stud_Fee FROM Student WHERE Stud_ID= s.Stud_ID];



Update myStud;

} catch (DmlException d) {

               Exception Code


Upsert Operation

This DML operation is used to update the existing record and creates a new record in a single statement.


Upsert vobject[optional_field]

	Upsert vobject[] [optional_field}

The upsert statement suits the sObjects with available records by equating values of one domain. While invoking the Upsert statement if we don’t mention the domain it uses the ID of the sObject to compare the sObject with the available records in the salesforce. If you specify the ID, you can use it for comparing.

For custom objects, we should mention a custom field labelled as external ID. For Standard objects, we have to mention a field which has the idLookup attribute determined to be true.

How Upsert selects Update or Insert
  • Upsert inserts a new record or updates an available record based on the ID of the sObject. Conditions for Update or Insert:
  • A new record is inserted, when the key is not similar
  • The available record is not updated when the key is not similar.
  • When the key is matched several times, an error will be generated and the record of the object cannot be updated or inserted.

Checkout Salesforce Interview Questions

Undelete Operation

This DML operation is used to restore the existing records of a standard object or a custom object.


Undelete vObject | Name

Merge Operation

This DML Operation is used to merge three records of the same object into one record. Merge operation is used to combine the records. 


Merge vObject vObject

Merge vObject ID

The first argument portrays the superior record to which the other records are merged. The second argument portrays one or two records that are to be merged and after that, they can be deleted. 


In salesforce, DML operations are performed to change the data according to our requirements. Dml operations play a key role in handling and formatting the data. DML plays a key role in Apex as it deals with crucial operations of the database. 

In the next topic, we will discuss in detail about “Governor Limits in Salesforce”. Keep following us for more info on Salesforce Development / Programming.

Are you looking to get trained on Salesforce, we have the right course designed according to your needs. Our expert trainers help you gain the essential knowledge required for the latest industry needs. Join our Salesforce Certification Training program from your nearest city.

Salesforce Training Chennai, Salesforce Training Dallas, Salesforce Training Bangalore, Salesforce Training Hyderabad, Salesforce Training Mumbai, Salesforce Training Delhi, Salesforce Training Noida, Salesforce Training New York, Salesforce Training Chicago, Salesforce Training Kolkata, Salesforce Training Gurgaon, Salesforce Training Pune.

These courses are equipped with Live Instructor-Led Training, Industry Use cases, and hands-on live projects. Additionally, you get access to Free Mock Interviews, Job and Certification Assistance by Certified Salesforce Trainers

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration TrainingSalesforce Lightning Training
Salesforce Advanced Developer TrainingSalesforce Developer Training
Salesforce IoT TrainingSalesforce App Builder Certification Training
Salesforce AppExchange TrainingSalesforce Service Cloud Training
and many more. 
Course Schedule
Salesforce TrainingMay 28 to Jun 12View Details
Salesforce TrainingJun 01 to Jun 16View Details
Salesforce TrainingJun 04 to Jun 19View Details
Salesforce TrainingJun 08 to Jun 23View Details
Last updated: 29 Jul 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read less
  1. Share:
Salesforce Articles