+ Start a Discussion
carramrodcarramrod 

Apex Class/Trigger hitting limits -need a quick lookover

i've got a trigger calling this, but it bombs everytime an account has over 20 contacts. I want to incorporate some custom objects similarly too, but i'm hitting limits.
I'm using this for an integration and need to fire an update on every child record (contact, custom object, etc..) when an account is saved. If i daisy chain the updates to other objects to other triggers, can i get around this limitation?
for instance: assuming what i have below can be fixed and i can update more than 20 contacts in one call, account trigger modifies contacts, contacts trigger modifies custom object a, custom object a modifies custom object b.

    public static void fireUpdatesOnChildObjects(Account[] accs){
        for (Account a:accs){
            //change status of this object.
            if (a.Toggle_Update__c == true){
                a.Toggle_Update__c = false;
            }   
            else{
                a.Toggle_Update__c = true;   
            }
            //fire updates on contacts
            Contact[] contacts = [SELECT ID,Toggle_Update__c FROM CONTACT WHERE AccountID =:a.ID];
            for (Contact c:contacts){
                if(c.Toggle_Update__c == true){
                    c.Toggle_Update__c = false;   
                }   
                else{
                    c.toggle_update__c = true;   
                }
                update c;
            }
            //fire updates on Trip Logs
            /*Trip_Log__c[] triplogs= [SELECT ID,Toggle_Update__c FROM Trip_Log__c WHERE Account__c =:a.ID];
            for (Trip_Log__c t:triplogs){
                if(t.Toggle_Update__c == true){
                    t.Toggle_Update__c = false;   
                }   
                else{
                    t.toggle_update__c = true;   
                }
                update t;
            }*/
        }
       
           
    }
ShamilShamil
Your main problem is that you are using DML statements inside a loop.
A couple of things you need to do to make it all work:

1. Pass a Trigger.newMap - a map of Accounts to your method as a second parameter from your trigger to the
fireUpdatesOnChildObjects method;
2. Modify your code in the following way, so that DML statements are no longer inside a loop:


Code:
public static void fireUpdatesOnChildObjects(Account[] accs, Map<Id,Account> mNewAccountsMap){
 for (Account a:accs){
  //change status of this object.
  if (a.Toggle_Update__c == true){
   a.Toggle_Update__c = false;
  }   
  else{
   a.Toggle_Update__c = true;   
  }
 }

 //fire updates on contacts
 List<Contact> lContactToUpdate = new List<Contact>();
 for(Contact c: [SELECT ID,Toggle_Update__c FROM CONTACT WHERE AccountID in :mNewAccountsMap.keySet()]){
  if(c.Toggle_Update__c == true){
   c.Toggle_Update__c = false;   
  }   
  else{
   c.toggle_update__c = true;   
  }
  lContactToUpdate.add(c);
 }
 update lContactToUpdate; 


 //fire updates on Trip Logs
 List<Trip_Log__c> lTripLogsToUpdate = new List<Trip_Log__c>();

 for(Trip_Log__c t : [SELECT ID,Toggle_Update__c FROM Trip_Log__c WHERE Account__c in :mNewAccountsMap.keySet()]){
  if(t.Toggle_Update__c == true){
   t.Toggle_Update__c = false;   
  }   
  else{
   t.toggle_update__c = true;   
  }
  lTripLogsToUpdate.add(t);
 }
 update lTripLogsToUpdate;
 
}