+ Start a Discussion
Sunny NarulaSunny Narula 

Please suggest Best Approach for bulkify trigger

I need to have a trigger on Accounts but getting stuck up in governor limits

As having the following scenario:
Parent Account has a related list that holds a list of child accounts for the respective parent account.
If a field named PortType is updated on Parent account, then this field is needed to be updated on all corresponding child accounts.

As I thought of creating a MAP (ID, Account), so as to fetch and update account from the MAP and then update them collecting in a list at once, but that did not work as Accounts records are too much that MAP is not able to hold and getting an error as MAP limit exceeds.

Getting following issues:

  1. Recursive update loop, as a child accounts are getting and a trigger is on account update.
  2. Account are many many, even if I think to take all account on a Map at once and execute, Map limit is getting exceeded as there are more then 500000 accounts in the ORG.

Please guide me how to process such bulk operations...

Sudha#aSudha#a
hi guys

 more than 5 lk Accounts... than go to Batch class using Database.query locator..... default methods in finish method ......
don't using map concept....
Maharajan CMaharajan C
Sunny post your code so that we can help
Sunny NarulaSunny Narula

another object that holds relationship for accounts

====================

public class AccountRelatedHandler {
    
    private static boolean run = true;  
    public static boolean runOnce()
    {
     if(run)
     {
     run=false;
     return true;
     }
     else{
     return run;
     }
}
                
    public void getReleatedAccounts(Set<ID> setAccIds)
    {       
        List <Account> mainAccountsList = [Select id, name, PortType__c, description from Account where id IN : setAccIds];
        Map<ID, Account> mapSourceA = new Map<ID, Account>();
        for(Account soA : mainAccountsList )
        {
            mapSourceA.put(soA.ID, soA);
        }
                                
        List <AccReationShip__c> releatedList = [select id, Source_Account__c, Related_Account__c from AccReationShip__c where Related_Account__c IN: setAccIds];
        Set<ID> sourceSet = new Set<ID>();
        Map<ID, ID> mapSR = new Map<ID, ID>();
        for(AccReationShip__c ar : releatedList)
        {
            sourceSet.add(ar.Source_Account__c);
            mapSR.put(ar.Source_Account__c, ar.Related_Account__c );            
        }                       
        
        List <Account> sourceAccountsList = [Select id, name, PortType__c, description from Account where id IN : sourceSet];               
        List <Account> finalAccountList = new List<Account>();
        for(Account sa : sourceAccountsList)
        {           
            ID rID = mapSR.get(sa.id);
            Account a = mapSourceA.get(rID);           
            sa.PortType__c = a.PortType__c;
            finalAccountList.add(sa);
            
        }               
        update finalAccountList;                
    }       
}

====================

Please let me know how can I more bulkify this wrapper class.

Else, please let me help how can I create a Batchable(if required) for this, as I have never created one.
 

thanks