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.
List accounts = new List();
accounts.add(new Account(Name = 'Test Account'));
accounts.add(new Account(Name = 'Test Account'));
System.debug('Account List Size : ' +accounts.size());
insert accounts;
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
// Instance OR Object
List accountList = new List();
Account accountRecord = new Account();
accountRecord.Name = 'Amazon.com';
accountRecord.Site = 'https://www.amazon.com';
accountRecord.WebSite = 'https://www.amazon.com';
accountRecord.Rating = 'Cold';
accountList.add(accountRecord); // 0 Index
Account accountRecord1 = new Account();
accountRecord1.Name = 'Flipkart.in';
accountRecord1.Site = 'https://www.Flipkart.in';
accountRecord1.WebSite = 'https://www.Flipkart.in';
accountRecord1.Rating = 'Hot';
accountList.add(accountRecord1);
Integer length = accountList.size(); // 2 , lastIndex length -1
// Partial DML is allwed
List saveResult = Database.insert(accountList, false);
// saveResult.get(0) // Amazon.com
for(Integer i=0; i
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
Map acctBinds = new Map{
'acctName' => 'Acme Corporation
};
List accts =
Database.queryWithBinds('SELECT Id FROM Account WHERE Name = :acctName',
acctBinds,
AccessLevel.USER_MODE);
Hands-On
public class SOQLDemo {
public void searchAccount(String accountName){
// Like
String likeParam = '%'+accountName+'%';
List accountList = [SELECT Id, Name FROM Account WHERE Name Like:likeParam];
System.debug(accountList);
}
public static void query(String objectApiName, String fieldsToReturn, Integer limitCount,
String filterCondition, Map variableBinds){
// Known information
// Object API Name
// Fields to Return
String queryString = 'SELECT '+fieldsToReturn+' FROM '+objectApiName+' WHERE '+filterCondition+' LIMIT '+limitCount;
System.debug(queryString);
// SELECT Id, Name FROM Account WHERE Name Like:likeParam LIMIT 5
List records = Database.queryWithBinds(queryString, variableBinds, AccessLevel.USER_MODE);
System.debug('records '+records);
/*
Map variableBinds = new Map();
variableBinds.put('likeParam', '%uni%');
variableBinds.put('accountName', 'Amazon.com');
SOQLDemo.query('Account', 'Id, Name', 5, 'Name Like:likeParam OR Name =:accountName ',variableBinds);
*/
}
}
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
public class SOSLDemo {
public static void find(String keyword){
List> records = [FIND :keyword IN ALL FIELDS
RETURNING
Account(Id, Name, Rating, Industry),
Contact(Id, Name, Email),
Opportunity(Id, Name, Amount),
Lead(Id, Name, Email, Company, Phone),
Case(Id, CaseNumber, Subject, Status, Priority),
User(Id, Email, Name, UserName, Title, Department)
];
System.debug(records.size());
List accountRecords = records.get(0);
System.debug(accountRecords);
List contactRecords = records.get(1);
System.debug(contactRecords);
}
}
[…] https://www.pantherschools.com/database-dml-methods-sosl-in-salesforce/ […]
[…] https://www.pantherschools.com/database-dml-methods-sosl-in-salesforce/ […]