function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
IvanWuIvanWu 

How to get all the Exceptions when execute the DML operation

hello ,everyone  ,i have a batch of records and i have put them into a list ,i used the DML to insert them,i warped up the DML in the try-catch
what i want is whenever there is a exception occured ,i can catch it,  at the same time ,the program can still run and catch  the exception constantly.
is there any method ? any sugguestions would be appreciate!
v varaprasadv varaprasad
Hi Ivan,

Please check sample code below.
 
// Create two accounts, one of which is missing a required field
Account[] accts = new List<Account>{
    new Account(Name='Account1'),
    new Account()};
Database.SaveResult[] srList = Database.insert(accts, 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());
        }
    }
}


More Info :
 http://www.salesforceadda.com/2017/08/insert-vs-databaseinsert-in-salesforce.html
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_saveresult.htm


Hope this helps you!​

Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com​