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
LiorGLiorG 

Exception handling on Bulk DML statements

I have a trigger on Opportunities that makes an update on a list of Accounts, after looping through the Opps. How do I handle an exception thrown at the DML statement - update Accounts and show the error on the right opp?

 

 

JHayes SDJHayes SD

Exception handling in Apex: http://wiki.developerforce.com/page/An_Introduction_to_Exception_Handling

 

Wrap the update statement in your trigger in a try-catch block and catch DMLException.

Noam.dganiNoam.dgani

Hi Lior

 

You can do something like the following:

 

lets say you have a List<Account> accounts - which is the list that you want to update.

 

do -

Database.saveResult[] saveResults = Database.update(accounts,false);

 

what this means is - 

1.do an update on the accounts list

2. if some fail, i dont want to fail the entire dml (thats the false argument, you can use true if it suits you better).

3. give me the results of the DML into an array of saveResults

 

Now you can do - 

for(Integer i = 0; i < saveResults.size(); i++)

{

//get the save result

Database.saveResult sr = saveResults[i];

if(!sr.isSuccess())// if the save was not successfull

{

//get the relevant account

Account acc = accounts[i];

//

do something to your opps

//

}

}

 

Hope this helps. if it does - please mark the issue as resolved.