Quantcast
Wednesday , March 27 2019
Home / Apex / Apex Programming for beginners / Data Manipulation Language

Data Manipulation Language

Data Manipulation Language

 

Data Manipulation Language is the way we perform CRUD operations in the database i.e. create, retrieve, update and delete records in the Salesforce Database.

DML operations are carried on within a transaction, which is either successfully completed or if an error is encountered then the whole transaction is rolled back.

 

All operations which can either start in a trigger, apex class method, or anonymous code block. When they run in a transaction they are considered as a single unit of operations.  They also include chaining of procedure which is called from inside a transaction operation, for ex a class method updates a record which in turn begins execution of the trigger. They are all part of a single transaction.

 

DML consists of following statements.

  1. Insert
  2. Update
  3. Upsert
  4. Delete
  5. Merge
  6. Undelete
  7. Converting Leads

 

 

DML can be performed on a single record or to a batch or collection of records. Salesforce allows only 150 DML iteration per transaction, therefore it is advised that we should perform DML on a collection rather on a single record, it improves the overall execution efficiency of the code.

 

Ex: inefficient code=>

For(account a: accountlist){

If(a.name ==”somevalue”){

a.phone = “9999999999”;

}

Update a;

}

 

Ex: More Efficient code=>

 

List<Account> acctoUpdate = new list<Account>();

For(account a: accountlist){

If(a.name ==”somevalue”){

a.phone = “9999999999”;

}

acctoUpdate .add(a);

}

Update acctoUpdate;

 

DML and Database Class methods

Apex has offered Data manipulation in two ways, DML operations or the use of Database class method.

 

Ex: DML

List<Account> accountList = new List<Account>();

accountList.add( new Account(Name=”Test1”));

accountList.add( new Account(Name=”Test2”));

 

insert accountList;

 

Database Class

 

List<Account> accountList = new List<Account>();

accountList.add( new Account(Name=”Test1”));

accountList.add( new Account(Name=”Test2”));

 

Database.SaveResult[] SaveResultList = Database.insert(accountList, false);

 

If the parameter passed in the method is true, then it behaves exactly same as the DML insert operation.

 

The main reason to use Database Class over the DML operation is the ability to partially process records if an error is encountered, in DML the whole transaction is roll backed in case of an exception.

 

You can use DML if you want to through Apex Exception immediately interrupting the control flow.

 

Next:  DML Operation in detail

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

Jitterbit Cloud agent with Salesforce

How to use Jitterbit Cloud agent with Salesforce

How to use Jitterbit Cloud agent with Salesforce     Have you ever found yourself …

MODAL Box

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 …

Leave a Reply

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