Database DML Methods & SOSL in Salesforce

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

  1. 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.
  2. You cannot get the list of successful and failed records.
  3. Example – insert, update

Database Methods

  1. 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
  2. You can get the list of successful and failed records as we have seen in the example.
  3. 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


					
				

Watch Complete Video

Resources

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

2 Comments

Leave a Reply

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

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading