Salesforce Apex Tutorial Chapter 17: Database Methods In Apex

Salesforce Apex Tutorial Chapter 17: Database Methods In Apex

On November 22, 2021, Posted by , In Salesforce Apex Tutorial, With Comments Off on Salesforce Apex Tutorial Chapter 17: Database Methods In Apex

Apex provides two ways to perform DML operations, DML statements and Database methods.

Following are the basic differences between the DML statements and the Database methods.

DML StatementsDatabase Methods
DML statements do not support the partial manipulation of records in a list.Database methods allow partial manipulation of records in a list.
DML does not return the list of success or failure of records.Database method returns the list of success and failure of records.
Syntax: Insert recordList;syntax: database.insert(recordList, false);
DML Statements Vs Database methods

Keynote: In database methods when errors are encountered, you can define whether to allow partial processing or not. To do this, you will need to pass an additional Boolean parameter in the method. 

If you specify False in the parameter that means if any of the records in a list got the error won’t affect the processing of other records and the remaining records will update successfully except that record. 

If you specify True in the parameter, that means if any of the records in the list got an error during the processing of records, the whole list will roll back and an exception will be thrown.

Let’s take an example that clears the picture of the above keynote.

// Create the list of sObject to insert
List<Account> acctList = new List<Account>();
acctList.add(new Account(Name='Acme1'));
acctList.add(new Account(Website =’www.salesforce.com'));
// Database Method Invocation.  As the additional parameter set to false it will allow partial processing
Database.SaveResult[] srList = Database.insert(acctList, false);
// Above invocation results in one success and one failure. The mandatory field has not been mapped for the second account so it will fail.
// Iterate through the returned result
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
System.debug('Successfully inserted account. Account ID: ' + sr.getId());
}
else {
// Operation failed for second account, so get all errors                
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');                    
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
}
}
// Create the list of sObjects to insert
List<Account> acctList = new List<Account>();
acctList.add(new Account(Name='Acme1'));
acctList.add(new Account(Website =’www.salesforce.com’));
// Database Method Invocation. As the additional parameter set to true it will not allow partial processing
Database.SaveResult[] srList = Database.insert(acctList,true);
// The above invocation results in  failure as name is not mapped for second account
// Iterate through each returned result
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
}
else {
// Operation failed, so get all errors                
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');                    
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
}
}

Comments are closed.