Thursday, November 8, 2018

DML Statement vs Database Class Method in Salesforce.com

DML Statement:
It does not support partial execution, which means if an error encountered an apex records execution at that moment whole execution will rolled back and throw error messages. Use try and catch block to handle exceptions and  throw error messages as well. 

Database Class Methods:

It supports partial execution, which means if an error encountered an apex records execution at that moment the records causing errors will be expelled from execution, and successfully executed record will be processed. 

Note: Database.Insert(Boolean);


Partial ==> Database.Insert (false)

No Partial ==> Database.Insert (true)

ApexCode1A


// Create the list of sObjects to insert
List<Account> acctList = new List<Account>();
acctList.add(new Account(Name='Acme1'));
acctList.add(new Account(Name='Acme2'));

// DML statement

insert acctList;

ApexCode1B


// Create the list of sObjects to insert
List<Account> acctList = new List<Account>();
acctList.add(new Account(Name='Acme1'));
acctList.add(new Account(Name='Acme2'));

// DML statement (Partial)

Database.SaveResult[] srList = Database.insert(acctList, false);

// Iterate through each returned result

for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account ID: ' + sr.getId());
    } 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());
        }
    }
}

SaveResult Methods (Instance Methods)

* getErrors()
* getId()
* isSuccess()










No comments:

Post a Comment

Validation in flow input text field in sfdc: Maximum 255 characters limit

The input text field validate in sfdc flow behaves opposite of the generic sfdc validation rule.  Here the validation formula is evaluating ...