As a Salesforce application developer, we always have a question like how many ways we can perform the DML operations when dealing with transactions. The answer is Salesforce provides 2 ways to perform the DML operations. Let us understand the difference between DML Statements and Database methods in Salesforce.
Table of Contents
Introduction
Let’s start with an example where we wanted to insert the account record.
Using the above approach if any account record insert fails then all the records will fail. so what if We wanted to save the records that were successfully processed and only fail those that failed? then we will use Database class methods for DML
Common Database Methods in Salesforce
- Insert
- Update
- Delete
- convertLead
- Query
Approach: All or nothing.
Syntax: Database.insert(List<sObject> object, Boolean);
Syntax: Database.update(List<sObject> object, Boolean);
Syntax: Database.delete(List<sObject> object, Boolean);
Hands-On
Database Class DML Methods vs Normal DML
DML Statements
- Partial DML is not allowed. For example, if you have 100 records in the list, then either all the records will be updated or none.
- You cannot get the list of successful and failed records.
- Example – insert, update
Database Methods
- A partial DML is allowed. You can specify the Parameter in the Database method as true or false, true to allow the partial update, and false for not allowing the same
- You can get the list of successful and failed records as we have seen in the example.
- Example – Database. insert, Database. update
Database Query Method
Database.Query method in Salesforce takes a single string parameter as a Query and returns the List<sObject> as a result.
Dynamic SOQL with Bind in Apex
Hands-On
SOSL in Salesforce
SOSL (Salesforce Object Search Language) is used to search a keyword in single or multiple objects. List<List<sObject>>
No DML is Allowed in the result of SOQL.
Hands-On

2 thoughts on “Database DML Methods & SOSL in Salesforce”
Comments are closed.