DML Operations in Apex | All You Need to Know

DML Operations in Apex: All You Need to Know

In this Blog, we will study DML Operations in Apex.

  • DML is used to insert, update, delete and undelete records. 
  • We can use upsert to either insert or update a record. 
  • We also use merge when duplicate leads, contacts and accounts are present and merge them into one record, others are deleted and related records are reparented. 
  • We should always perform DML operations in bulk. 
  • We can handle exceptions. 

Insert Records 

  • Example of Inserting Only One Record 
Account acc = new Account(Name=’Tech School’, Phone=’123456’); 
insert acc;
  • Fetch the Id of the Inserted Record 
System.debug(‘Acc ID = >‘ +;
  • Inserting Records in Bulk using List 
List<Account> accList = new List<Account>(); 
Account acc = new Account(Name=’record1’ , Phone=’12345’); 
Account acc1=new Account(Name=’record2’ , Phone=’12543'); 
Insert accList;
  • Governor Limit Check
    • Inserting two records separately then two DML will be counted. 
    • But if we insert two records through a list then one DML will be counted. 
  • Inserting Related Records, for example first insert account and then contact
Account acc = new Account(Name=’record1’ , Phone=’12345’); 
insert acc; 
Contact con = new Contact(Name=’abc’ , LastName=’xyz’ ,; 
insert con;

dont miss out iconDon't forget to check out: Best Practices to Avoid Excessive SOAP and REST API DML | Salesforce Developer Guide

Update Records 

  • Query an Existing Record and Print its Values
Account acc= [Select Id,Name,Phone FROM Account Where Name=’record1’]; 
  • Then, Update the Record and do Update DML’654321’; 
update acc;
  • Now, Query the Updated Record
Account updatedAcc=[Select Id,Name,Phone FROM Account Where Name=’record1’];
  • Now apply System.assertEqual to Validate Update 
  • Update Related Records
Contact con = [Select ID,FirstName,LastName,Phone, From Contact where FirstName=’abc’ AND LastName=’xyz’ and AccountId!=null);’12365’; 
update con; 
update con.Account;

So, in this way firstly contact record will be updated, and after that its related account will also be updated. 

dont miss out iconCheck out another amazing blog by Bhawana here: Learn All About Collections in Salesforce: List, Set and Map

Upsert Records 

This operation allows us to Create one record and update the existing one using upsert DML. 

Here we can use a list to upsert records. 

List<Account> accList=new List<Account>(); 
Account acc1= new Account(Name=’record1’,Phone=’12765’); 
Account acc2=[Select ID,Name,Phone FROM Account WHERE Name=’record2’]; 
Upsert accList;

Merge Records 

  • So only lead, Contact and Account records can be merged. 
  • We can only merge up to three records of the same sObject type into one. 
  • For example, if three leads have the same value then we can merge them into one same goes for contact and account records. 
  • After merging the records, the old record will be deleted and merged into a new one. 

So, record 1 has related contact and record 2 does not have any related contact then if we merge them then we can have related contact in record 2 as well. 

Account mergeinto=[Select ID,Name,Phone FROM Account WHERE Name=’record2’]; 
Account mergefrom=[Select ID,Name,Phone FROM Account WHERE Name=’record1’]; 
Merge mergeinto mergefrom;

Delete Records 

  • Apply SOQL. 
  • Now delete queried records using delete DML. 
Account acc= [Select Id,Name,Phone FROM Account Where Name=’record1’]; 
delete acc; 
And to delete more than one record you can create list. 
List<Account> acc= [Select Id,Name,Phone FROM Account Where Name=’record1’]; 
delete acc;

Undelete Records 

  • Insert a record. 
  • Delete that record. 
  • Now query deleted records using all rows in the query. 

All rows will query all the records whether they are deleted or undeleted. 

  • Then apply undelete statement. 
Account deletedacc= [Select Id,Name,Phone FROM Account Where Name=’record1’ ALL ROWS]; 
undelete deletedacc;

DML Statement Exception 

Apply to try and catch to handle exceptions raised when DML operations fail. 

Account acc = new Account(); 
insert acc; 
catch(DMLException e){ 
System.debug(‘Error=>' + e.getMessage()); 

So, this will show all the DML errors which will appear while performing DML operations inside the org. Such as here, in this case, we can see that the account record is created without a name so it will show an error. 


Popular Salesforce Blogs