You need to sign in to do that
Don't have an account?
![LiorG LiorG](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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?
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.
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.