
Understanding DML in Salesforce Apex

Table of contents
DML, which stands for Data Manipulation Language, is like the control panel for making changes to your Salesforce data. Think of it as a set of tools that allows you to insert, update, or delete records in your Salesforce database. It’s how you interact with and modify your data.
Why DML Is Important?
Imagine you’re managing a list of customer contacts, and you need to add a new contact, update existing information, or remove outdated records. DML is essential because it provides the commands and functions to perform these actions efficiently and accurately. It’s like having the ability to add, edit, or remove items from your to-do list.
Checkout: DML statements in Salesforce
Types of DML Operations
DML in Apex consists of several operations:
- Insert – Creates new records.
- Update – Modifies existing records.
- Upsert – Inserts or updates records based on a unique key.
- Delete – Deletes records.
- Undelete – Recovers deleted records from the Recycle Bin.
- Merge – Merges up to three records.
Read more: SOQL Query in Salesforce
Insert Statement
The insert operation is used to add new records to a standard or custom object. If any record in the list fails, the entire transaction is rolled back.
Syntax:
insert sObject;
insert sObject[];Example:
// Insert a single Account record
Account acc = new Account(Name = 'ABC Pvt Ltd');
insert acc;
// Insert multiple Account records
List<Account> accList = new List<Account>();
accList.add(new Account(Name = 'XYZ Pvt Ltd'));
accList.add(new Account(Name = 'LMN Pvt Ltd'));
insert accList;Update Statement
The update operation modifies existing records in Salesforce. You can update a single record or a list of records at once, but partial execution is not allowed.
Syntax:
update sObject;
update sObject[];Example:
// Fetch an existing account and update its Type
Account existingAcc = [SELECT Id, Name FROM Account WHERE Name = 'ABC Pvt Ltd' LIMIT 1];
existingAcc.Type = 'Prospect';
update existingAcc;
// Bulk update
List<Account> accList = [SELECT Id, Name FROM Account WHERE Name IN ('XYZ Pvt Ltd', 'LMN Pvt Ltd')];
for (Account acc : accList) {
acc.Type = 'Prospect';
}
update accList;Upsert Statement
The upsert operation creates new records if they do not exist or updates them if they do. It requires either the Id field or an External ID to identify records.
Syntax:
upsert sObject;
upsert sObject field;
upsert sObject[];Example:
// Upsert using Id
Account acc = new Account(Name = 'PQR Pvt Ltd');
upsert acc;
// Upsert with External Id
upsert acc External_Id__c;Check out these top Salesforce interview questions and answers for extensive knowledge and informative details about Salesforce Admin, Developer, Integration, and LWC modules.
Delete Statement
The delete operation removes records permanently from Salesforce unless they are recoverable from the Recycle Bin. It can be applied to single or multiple records.
Syntax:
delete sObject;
delete sObject[];Example:
// Delete a single record
Account accToDelete = [SELECT Id FROM Account WHERE Name = 'ABC Pvt Ltd' LIMIT 1];
delete accToDelete;
// Delete multiple records
List<Account> accListToDelete = [SELECT Id FROM Account WHERE Name IN ('XYZ Pvt Ltd', 'LMN Pvt Ltd')];
delete accListToDelete;Undelete Statement
The undelete operation restores records from the Recycle Bin within 15 days of deletion, provided they haven’t been permanently removed.
Syntax:
undelete sObject;
undelete sObject[];Example:
// Restore deleted accounts
List<Account> deletedAccounts = [SELECT Id FROM Account WHERE IsDeleted = true ALL ROWS];
undelete deletedAccounts;Merge Statement
The merge operation combines up to three records into one, preserving the master record while reparenting related records. It fires delete and update events but not a merge event.
Syntax:
merge sObject;
merge sObject[];Example:
// Merge duplicate Accounts
Account masterAcc = [SELECT Id FROM Account WHERE Name = 'ABC Pvt Ltd' LIMIT 1];
List<Account> duplicates = [SELECT Id FROM Account WHERE Name = 'ABC PTY Ltd.' OR Name = 'ABC Pvt Ltd.' LIMIT 2];
merge masterAcc duplicates;Example of DML in Apex
Here’s a simple example of how DML works in Apex:
// Creating a new Contact record
Contact newContact = new Contact();
newContact.FirstName = 'John';
newContact.LastName = 'Doe';
newContact.Email = 'john.doe@email.com';
// Inserting the new Contact record into the database
insert newContact;
// Updating the Contact record
newContact.Email = 'new.email@email.com';
// Updating the Contact record in the database
update newContact;
// Deleting the Contact record
delete newContact;In this code, we first create a new Contact record, insert it into the database, update its email address, and then delete it.
Common Use Cases
DML operations are essential for various tasks in Salesforce, including:
- Adding new records when new customers or data are entered.
- Updating records when information changes.
- Removing records when they are no longer needed.
- Automating data management processes using triggers and workflows.
Understanding how to use DML operations effectively in Apex is crucial for managing your Salesforce data accurately and efficiently. Whether you’re a developer, administrator, or business user, mastering DML helps you keep your data organized and up to date.
CRS Info Solutions offers real-time Salesforce course for beginners designed to equip learners with practical knowledge and industry skills in Salesforce. Enroll for demo today.

