+ Start a Discussion

account trigger exception

the below class is invoked from before insert/update trigger.  This code stub was on another  thread. Any reason why when i hit this exception it can not be caught?


public static void AccountOptOutContUpdate(List<Account> aNewList, List<Account> aOldList)
Map<Id, Account> acctsMap= new Map<Id, Account>();
List<Contact> updatedContacts = new List<Contact>();
Account parentAccount = new Account();
for (Integer i = 0; i < Trigger.new.size(); i++)
if(aOldList.get(i).Do_Not_Call__c != aNewList.get(i).Do_Not_Call__c || aOldList.get(i).Email_Opt_Out__c != aNewList.get(i).Email_Opt_Out__c
|| aOldList.get(i).Fax_Opt_Out__c != aNewList.get(i).Fax_Opt_Out__c)
acctsMap.put(aOldList.get(i).Id, aNewList.get(i));
for (Contact c : [SELECT id, accountId, HasOptedOutOfFax, HasOptedOutOfEmail, DoNotCall FROM contact WHERE accountId in :acctsMap.keySet()])
if(c.DoNotCall ==false || c.HasOptedOutOfEmail == false || c.HasOptedOutOfFax == false)
parentAccount = acctsMap.get(c.accountId);
c.DoNotCall = parentAccount.Do_Not_Call__c;
c.HasOptedOutOfEmail = parentAccount.Email_Opt_Out__c;
c.HasOptedOutOfFax = parentAccount.Fax_Opt_Out__c;
update updatedContacts;
// }
/* catch(System.LimitException ex)
parentAccount.addError('unexpected error occur, please call your system admin!');




One thing I can think of is that you are specifically catching LimitException. You might want to add a catch-block för generic exceptions as well. Here's some info from developerForce:


You "try" to run your code. If there is an exception, you "catch" it and can run some code, then you can "finally" run some code whether you had an exception or not.

You can have multiple Catch blocks to catch any of the 20 different kinds of exceptions. If you use a generic exception catcher, it must be the last Catch block.

Here's what a try-catch-finally block looks like:


//Your code here
} catch (ListException e) {
//Optional catch of a specific exception type
//Specific exception handling code here
} catch (Exception e) {
//Generic exception handling code here
} finally {
//optional finally block
//code to run whether there is an exception or not


HTH / Niklas