Data Manipulation Language(DML) in Salesforce Apex : Bijay Kumar
by: Bijay Kumar
blow post content copied from SalesForce FAQs
click here to view original post
**Summary of Data Manipulation Language (DML) in Salesforce Apex** In Salesforce, data is retrieved using SOQL (Salesforce Object Query Language) queries. However, when you need to manipulate that data—such as inserting, updating, or deleting records—you use DML (Data Manipulation Language) in Salesforce Apex, which is Salesforce's programming language. **Key Details:** 1. **What is DML?** - DML stands for Data Manipulation Language. It allows users to perform operations like insert, update, delete, and undelete records in Salesforce. 2. **Key DML Operations:** - **INSERT**: Add new records. - **UPDATE**: Modify existing records. - **DELETE**: Remove records (which can be restored from the recycle bin for 15 days). - **UNDELETE**: Restore deleted records. - **UPERTS**: Insert or update records based on whether they already exist. 3. **Syntax for DML Operations:** - The basic syntax involves specifying the operation (e.g., INSERT, DELETE) followed by the record(s) (either a list or an individual sObject). 4. **Examples:** - **Inserting a Single Record**: Create a new Account. - **Inserting Multiple Records**: Create multiple Contact records using a list. - **Updating a Record**: Modify existing Account information. - **Deleting a Record**: Remove an Account and verify its deletion. - **Restoring a Deleted Record**: Recover a deleted Account from the recycle bin. 5. **Conclusion**: - Understanding DML is crucial for managing data within Salesforce, as it facilitates essential CRUD (Create, Read, Update, Delete) operations on Salesforce records. **Additional Context:** - DML operations are fundamental for any Salesforce developer to master, as they directly impact data integrity and functionality within the Salesforce platform. Since records can be restored from the recycle bin, it offers a safety net against accidental deletions. **SEO Hashtags:** #Salesforce #DML #Apex #DataManipulation #SalesforceDevelopment #SOQL #CRUD #SalesforceApex #SalesforceTutorials #SalesforceRecords
In Salesforce, to retrieve the data from the Salesforce org, we used a SOQL query. But when there is a need to perform operations such as Insert, Update, and Delete on retrieved records
Like other programming languages, Salesforce also has a programming language called DML (Data Manipulation Language) in Salesforce Apex. The DML allows users to perform data operations on Salesforce records.
In this tutorial, we will learn about Data Manipulation Language (DML) in Salesforce Apex. Additionally, I will explain the operations that can be performed in DML and how to execute DML operations within Apex classes.
What is DML in Salesforce Apex?
DML stands for Data Manipulation Language, which allows users to insert, update, delete, and Undelete records in Apex.
DML allows us to perform operations on a single sObject record or in bulk on a list of records simultaneously. DML is a very important part of Apex for updating Salesforce databases.
What is DML used for?
Using the DML operations, we can perform the following operation in Salesforce Apex:
- Insert new records in Salesforce.
- Update existing records.
- Upsert records.
- Delete records from the Salesforce database.
- Undelete or restore records from the recycle bin.
Syntax: Using DML Operation in Apex
DML_Operation List or sObject;
- DML_Operation: Here, we need to specify which operation we want to perform. For example, DELETE or UNDELETE data in the Salesforce database.
- List: Provide a list collection variable name where you have stored data on which you want to perform the DML operation.
- sObject: Provide the standard or custom object name on which you want to perform the DML operation.
INSERT DML Operation in Apex
The insert operation is used to add new records to the Salesforce database. Using the Insert DML statement, we can create records of any standard or custom object.
Example 1: Create New sObject Record Using DML Statement in Apex
In the example below, I explain how to use the INSERT DML operation to create a new record for an sObject.
public class Demo {
public void UseDML() {
Account acc = new Account(Name = 'DML Account');
INSERT acc;
}
}
In the above code, we created a sObject(Account) instance and provided the value for the name field.
After that, write the INSERT DML statement and provide the object instance to create or insert the account record in Salesforce.
In the image below, you can see that the account record has been successfully created.

Example 2: Create Multiple New Records Using DML Statement in Apex
To create multiple records using the INSERT DML operation, we need to use a list collection.
In the code below, we first declared a list to store the records we want to insert, allowing us to perform a single INSERT operation.
After that, we need to create a sObject instance to add values to the field or to create the record. After providing values, we need to add that instance to the list that we created.
We need to create instances according to the number of records we have to create.
public class Demo {
public void UseDML() {
List<Contact> contactList = new List<Contact>();
Contact con1 = new Contact();
con1.FirstName = 'First';
con1.LastName = 'Contact';
contactList.add (con1);
Contact con2 = new Contact();
con2.FirstName = 'Second';
con2.LastName = 'Contact';
contactList.add (con2);
INSERT contactList;
}
}
In the image below, you can see the contacts have been successfully created using the name we provided.

UPDATE DML Operation in Apex
Update operations are used to modify existing records stored in the Salesforce database. First, you have to query the existing records from the database using SOQL to perform an update operation on them.
Example: Use the UPDATE DML Operation in Apex
In this example, we are querying the account that we inserted in the previous insert operation example and will update its name and other fields.
In the below image, you can see we created an account record named ‘DML Account’ using the INSERT operation in the above example.
Now we will update the account name, account priority, and rating fields using the UPDATE DML operation.

In the Apex class below, we first fetched the account record that we want to update. Then, we updated the fields and, using the UPDATE DML statement, updated the specific record in the Salesforce database.
After that, we retrieved the updated account information using another SOQL query and displayed it using the debug method.
public class Demo {
public void UseDML() {
Account accountRecord = [ Select Id, Name from Account where Name = 'DML
Account' LIMIT 1 ];
accountRecord.Name = 'Updated DML Account';
accountRecord.Account_Priority__c = 'High';
accountRecord.Ratings_0_5__c = 4;
UPDATE accountRecord;
Account acctRecord =[ Select Id, Name from Account where Name = 'Updated DML
Account' LIMIT 1 ];
system.debug( 'account afterUpdate::: ' +acctRecord );
}
}
In the image below, you can see that the account record has been updated with the values provided in the Apex code.

Delete and Undelete DML Operations in Salesforce Apex
Below, I will explain what the Delete and Undelete DML statements are in Apex and how to use them to remove unnecessary records and restore deleted records in Salesforce Apex.
DELETE DML Operation in Apex
Delete operations are performed on the existing records in the Salesforce database. Deleted records are not permanently deleted from Salesforce.
After deleting the records, they are stored in the recycle bin for 15 days, after which they can be restored within the same timeframe.
Now, let’s consider a scenario where I want to delete the account named ‘Updated DML Account‘.
Before that, you can see that when we search for an account in the global search, the account exists in the account object.

In the Apex code below, we first retrieve the account record that we want to delete. Then, using the DELETE DML operation, we will delete that record.
After that, we declared the list collection in which we will store the account record that we deleted. If an account is deleted, this list will be empty; otherwise, it will contain some records.
Then, using an if condition, we checked whether the list is empty, then the account gets successfully deleted.
public class Demo {
public void UseDML() {
Account accDelete =[ Select Id, Name from Account where Name = 'Updated DML Account' LIMIT 1 ];
DELETE accDelete;
List<Account> accList = new List<Account>();
accList = [ Select Id, Name from Account where Name = 'Updated DML Account' LIMIT 1 ];
if (accList.size() == 0){
system.debug( 'account does not exist' );
}
else{
system.debug( 'account existed' );
}
}
}
In the Execution Log, the result ‘account does not exist’ means the record was successfully deleted.

Additionally, if you search again for the deleted account record name in the global search, it will not appear in the search results.

In this way, we can use the DELETE DML operation in Salesforce Apex to delete the records.
UNDELETE DML Operation in Apex
If you accidentally delete a record in Salesforce, you can restore it using the undelete operation in Salesforce Apex, which is designed to recover deleted records.
When you delete records, they are stored in the Recycle Bin for 15 days. After 15 days, they are permanently deleted from Salesforce.
To query the deleted records from the recycle bin, we have to use the ALL ROWS parameter in the SOQL query.
For example, we want to retrieve the account records that we deleted in the above example of the delete operation. To restore the deleted account, we need to use the UNDELETE DML operation in Apex.
In the above DELETE DML operation, we deleted the ‘Updated DML Account’. Now, we want to restore this account using the UNDELETE DML operation.
In the Apex code below, we first retrieve the account record that we want to restore. Additionally, we need to use the ALL ROWS clause to restore all deleted fields. Then, using the UNDELETE DML operation, we restored that record.
public class Demo {
public void UseDML() {
Account accRestore = [ select Id, Name from Account where Name = 'Updated DML Account' ALL ROWS ];
UNDELETE accRestore;
Account accn = [ select Id, Name from Account where Name = 'Updated DML Account' ];
system.debug( 'restored account record : ' +accn );
}
}
In the Execution Log, the result displayed the account information that had been restored.

Again, as you search for the restored account name in the global search, it will appear in the search results.

In this way, we can use the UNDELETE DML operation in Salesforce Apex to restore the records.
Conclusion
I hope you have got an idea about the Data Manipulation Language(DML) in Salesforce Apex. I have explained DML in Salesforce, including its uses, syntax, various types of DML operations with examples, and the DML queries in Salesforce Apex.
You may like to read:
- How to Create Dynamic SOSL Query in Salesforce Apex
- Salesforce Object Search Language (SOSL) in Apex
- Salesforce DML Before and After Callout
- Salesforce SOQL and SOSL Limits for Search Queries
The post Data Manipulation Language(DML) in Salesforce Apex appeared first on SalesForce FAQs.
April 29, 2025 at 04:55PM
Click here for more details...
=============================
The original post is available in SalesForce FAQs by Bijay Kumar
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment