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
MaheemSamMaheemSam 

Bulkify the code in helper class

Hi,

  I have a code below which is working perfect in saleforce need suggestion make it bulkified. Please suggest me how to modify 
// Working Code

Public class ContactCertificateRollupHandlerNew1{
 
Public static Map<id,integer> NSEmap1;
Public static Map<id,integer> NSEmap2;
Public static Map<id,integer> NSEmap3;
Public static Map<id,integer> NSEmap4;
Public static Map<id,integer> NSEmap5;
Public static Map<id,integer> NSEmap7;
Public static Map<id,integer> NSEmap8;

Public static Map<id,integer> NSEmap6;

Public static Integer NSEint1 = 0;
Public static Integer NSEint2 = 0;
Public static Integer NSEint3 = 0;
Public static Integer NSEint4 = 0;
Public static Integer NSEint5 = 0;
Public static Integer NSEint7 = 0;
Public static Integer NSEint8 = 0;

Public static Integer NSEint6 = 0;

Public static List<account> NSE1_Update = new List<account>();
Public static List<account> NSE2_Update = new List<account>(); 
Public static List<account> NSE3_Update = new List<account>();
Public static List<account> NSE4_Update = new List<account>();
Public static List<account> NSE5_Update = new List<account>();
Public static List<account> NSE7_Update = new List<account>();
Public static List<account> NSE8_Update = new List<account>();

Public static List<account> NSE6_Update = new List<account>();
 
Public static map<id,account> NSE1_Update_Map = new map<id,account>();
Public static map<id,account> NSE2_Update_Map = new map<id,account>();
Public static map<id,account> NSE3_Update_Map = new map<id,account>();
Public static map<id,account> NSE4_Update_Map = new map<id,account>();
Public static map<id,account> NSE5_Update_Map = new map<id,account>();
Public static map<id,account> NSE7_Update_Map = new map<id,account>();
Public static map<id,account> NSE8_Update_Map = new map<id,account>();

Public static Account LastAccount; 

public class CertRollupSum {
        Integer Nse6 = 0;  
        Boolean NSE_6_FortiMail = false;
        Boolean NSE_6_FortiWeb = false;
        Boolean NSE_6_FortiWireless = false;
        Boolean NSE_6_FortiAuthenticator = false;
        Boolean NSE_6_FortiADC_D = false;
        Boolean NSE_6_FortiADC_E = false;
        Boolean NSE_6_FortiDDos = false;
        Boolean NSE_6_FortiSandbox = false;
        Boolean NSE_6_FortiWireless_Controller = false;
    }
     
public static void ProcessInsert(List<Contact> newCntLst){
   
    set<id> actidSet = new set<id>();
       
     for(Contact cont : newCntLst){
       actidSet.add(cont.AccountId);                                 
      }  
 }
  
 public static void ProcessDelete (List<Contact> oldCntLst){
 
     list<id> actidlst = new list<id>();       
     for(Contact cont : oldCntLst){
       actidlst.add(cont.AccountId);                                       
      }  
      GetLastAccounts(actidlst[0]);        
 } 
 
 public static void ProcessUpdate (Map<id, Contact> newMap, Map<id, Contact> oldMap){
 
    set<id> actidSet = new set<id>();
    list<id> actidlst = new list<id>();
    
    for(Contact cont : newMap.values()){
          actidlst.add(cont.AccountId);
      }                    
 
        GetLastAccounts(actidlst[0]); 
    
       
  }
     
     
  public static void GetLastAccounts (Id AccountID){    
   
    set<id> GetAllActID = new set<id>();

    GetAllActID = GetChildToParnet(AccountID);
    GetAllActID = GetParnetToChild(AccountID);
    
    system.debug('ChildToParent ' + AccountHierarchyUtil.ChildToParnet(AccountID));
    system.debug('ParenttoChild ' + AccountHierarchyUtil.ParnetToChild(AccountID));
    
    if( AccountHierarchyUtil.ParnetToChild(AccountID).size() > 0 ) {
      List<Account> LastAct = [SELECT Id, name FROM Account where id in :GetAllActID];
  
      LastAccount  = LastAct[LastAct.size() - 1]; 
        accountRollupUpdate(LastAccount.id);
     } else {
       accountRollupUpdate(AccountID);
      
     }   
     
   }
   
  
   public static void accountRollupUpdate(Id PAccountID) {
        
        Id accountId = PAccountID;
        
        NSEmap1 = new Map<id,integer>();
        NSEmap2 = new Map<id,integer>();
        NSEmap3 = new Map<id,integer>();
        NSEmap4 = new Map<id,integer>();
        NSEmap5 = new Map<id,integer>();
        NSEmap7 = new Map<id,integer>();
        NSEmap8 = new Map<id,integer>();
        NSEmap6 = new Map<id,integer>();

        ChildToParnet(accountId);

        if(NSEmap1.size()>0){
    
            For(Id i : NSEmap1.keyset()){        
                Account ains = new Account(); 
                ains.id = i;
        
                system.debug('Account' + i);       
                system.debug(NSEmap1.get(i));
        
                ains.NSE_1_Status__c = NSEmap1.get(i);
                   NSE1_Update.add(ains);                      
            }
        }


       if(NSEmap2.size()>0){
    
           For(Id i : NSEmap2.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap2.get(i));
        
              ains.NSE_2_Status__c = NSEmap2.get(i);        
                NSE2_Update.add(ains);
                      
           }
        }
        
       if(NSEmap3.size()>0){
    
           For(Id i : NSEmap3.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap3.get(i));
        
              ains.NSE_3_Status__c = NSEmap3.get(i);        
                NSE3_Update.add(ains);
                      
           }
        }
          
       if(NSEmap4.size()>0){
    
           For(Id i : NSEmap4.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap4.get(i));
        
              ains.NSE_4_Status__c = NSEmap4.get(i);        
                NSE4_Update.add(ains);
                      
           }
        }  
        
       if(NSEmap5.size()>0){
    
           For(Id i : NSEmap5.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap5.get(i));
        
              ains.NSE_5_Status__c = NSEmap5.get(i);        
                NSE5_Update.add(ains);
                      
           }
        }   
        
       if(NSEmap7.size()>0){
    
           For(Id i : NSEmap7.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap7.get(i));
        
              ains.NSE_7_Status__c = NSEmap7.get(i);        
                NSE7_Update.add(ains);
                      
           }
        }  
        
       if(NSEmap8.size()>0){
    
           For(Id i : NSEmap8.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap8.get(i));
        
              ains.NSE_8_Status__c = NSEmap8.get(i);        
                NSE8_Update.add(ains);
                      
           }
        }                            

       if(NSEmap6.size()>0){
    
           For(Id i : NSEmap6.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap6.get(i));
        
              ains.NSE_6_Status__c = NSEmap6.get(i);        
                NSE6_Update.add(ains);
                     
           }
        }     
                
        if(NSE1_Update.size()>0)           
          
          update NSE1_Update;

        if(NSE2_Update.size()>0)
        
          update NSE2_Update;  
          
        if(NSE3_Update.size()>0)
        
          update NSE3_Update;  
          
        if(NSE4_Update.size()>0)
        
          update NSE4_Update;   
          
        if(NSE5_Update.size()>0)
        
          update NSE5_Update;  
          
        if(NSE7_Update.size()>0)
        
          update NSE7_Update;   
          
        if(NSE8_Update.size()>0)
        
          update NSE8_Update;                        
            
        if(NSE6_Update.size()>0)
        
          update NSE6_Update; 
              
          /* map<id,account> nse1map = new map<id,account>();
            nse1map.putall(NSE1_Update);
        
            if(nse1map.size()>0){
            update nse1map.values();         
            }
            
            map<id,account> nse2map = new map<id,account>();
            nse2map.putall(NSE2_Update);
        
            if(nse2map.size()>0){
            update nse2map.values();
            }    */        
            
        
   }


public static void ChildToParnet (Id ChildId){
     
    For(Account acc : [select id,name, parentid,NSE_6_Status__c, (Select id,NSE_1__c,NSE_2__c,NSE_3__c,NSE_4__c,NSE_5__c,NSE_7__c,NSE_8__c,
                                                                         NSE_6_FortiMail__c, NSE_6_FortiWeb__c, NSE_6_FortiWireless__c, 
                                                                         NSE_6_FortiAuthenticator__c, NSE_6_FortiADC_D__c, NSE_6_FortiADC_E__c, NSE_6_FortiDDos__c, 
                                                                         NSE_6_FortiSandbox__c,NSE_6_FortiWireless_Controller__c 
                                                  from contacts)from account where id =: ChildId]){
        CertRollupSum rollup = new CertRollupSum();
        For(Contact c : acc.contacts ){
          
            if(c.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;                                 
            
            if(c.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;  
                
            if(c.NSE_3__c == true)
                NSEint3 = NSEint3 + 1;    
               
            if(c.NSE_4__c == true)
                NSEint4 = NSEint4 + 1;  
                
            if(c.NSE_5__c == true)
                NSEint5 = NSEint5 + 1; 
                
            if(c.NSE_7__c == true)
                NSEint7 = NSEint7 + 1;   
                
            if(c.NSE_8__c == true)
                NSEint8 = NSEint8 + 1;     
                
              if(c.NSE_6_FortiMail__c && !rollup.NSE_6_FortiMail) {
                    rollup.NSE_6_FortiMail = true;
                    rollup.Nse6 += 1;
                    //NSEint6 = rollup.Nse6; // Single rollup
                    NSEint6 = NSEint6 + 1;   // Multiple rollup
                }
                if(c.NSE_6_FortiWeb__c && !rollup.NSE_6_FortiWeb) {
                    rollup.NSE_6_FortiWeb = true;
                    rollup.Nse6 += 1;
                    //NSEint6 = rollup.Nse6;
                    NSEint6 = NSEint6 + 1;
                }
                if(c.NSE_6_FortiWireless__c && !rollup.NSE_6_FortiWireless) {
                    rollup.NSE_6_FortiWireless = true;
                    rollup.Nse6 += 1;
                    //NSEint6 = rollup.Nse6;
                    NSEint6 = NSEint6 + 1;
                }
                if(c.NSE_6_FortiAuthenticator__c && !rollup.NSE_6_FortiAuthenticator) {
                    rollup.NSE_6_FortiAuthenticator = true;
                    rollup.Nse6 += 1;
                    //NSEint6 = rollup.Nse6; 
                    NSEint6 = NSEint6 + 1;
                }
                if(c.NSE_6_FortiADC_D__c && !rollup.NSE_6_FortiADC_D) {
                    rollup.NSE_6_FortiADC_D = true;
                    rollup.Nse6 += 1;
                    //NSEint6 = rollup.Nse6;
                    NSEint6 = NSEint6 + 1;
                }
                if(c.NSE_6_FortiADC_E__c && !rollup.NSE_6_FortiADC_E) {
                    rollup.NSE_6_FortiADC_E = true;
                    rollup.Nse6 += 1;
                    //NSEint6 = rollup.Nse6;
                    NSEint6 = NSEint6 + 1;
                }
                if(c.NSE_6_FortiDDos__c && !rollup.NSE_6_FortiDDos) {
                    rollup.NSE_6_FortiDDos = true;
                    rollup.Nse6 += 1;
                    //NSEint6 = rollup.Nse6;
                    NSEint6 = NSEint6 + 1;
                }
                if(c.NSE_6_FortiSandbox__c && !rollup.NSE_6_FortiSandbox) {
                    rollup.NSE_6_FortiSandbox = true;
                    rollup.Nse6 += 1;
                    //NSEint6 = rollup.Nse6;
                    NSEint6 = NSEint6 + 1;
                }
                if(c.NSE_6_FortiWireless_Controller__c && !rollup.NSE_6_FortiWireless_Controller) {
                    rollup.NSE_6_FortiWireless_Controller = true;
                    rollup.Nse6 += 1;
                    //NSEint6 = rollup.Nse6;
                    NSEint6 = NSEint6 + 1;
                }               
                
                                                                                                             
            
        }
           NSEmap1.put(acc.id,NSEint1);
           NSEmap2.put(acc.id,NSEint2);  
           NSEmap3.put(acc.id,NSEint3);
           NSEmap4.put(acc.id,NSEint4);
           NSEmap5.put(acc.id,NSEint5);
           NSEmap7.put(acc.id,NSEint7);
           NSEmap8.put(acc.id,NSEint8);            
           NSEmap6.put(acc.id,NSEint6);
           
        if(acc.parentid != null)            
           childToParnet(acc.parentid);
        
     }
  }
  
   public static set<id> GetChildToParnet (Id PAccountID){
        
        set<id> setactid = new set<id>();
        
        Id accountId = PAccountID;
        
        Account[] allparents = new Account[] {};
            
            Set<Id> parentIds = new Set<Id>{accountId};
                
                Account[] parent;
        
        do {
            
            parent = [select Id,ParentId, Name from Account where Id in :parentIds];
            
            allparents.addAll(parent);
            
            parentIds.clear();
            
            for (Account par : parent) 
                
                parentIds.add(par.ParentId);
            
        } while (parent.size() > 0);
        
        list<Account> Act = [select id, name from account where id in :allparents];
        
        for(Account A : Act){
            
            system.debug('Parent Accounts ' + a.name);  
            setactid.add(a.id);
            
        }      
        
        return setactid;
        
    }
    
    public static set<id> GetParnetToChild (Id PAccountID){
        
        set<id> setactid = new set<id>();
        
        Id accountId = PAccountID;
        
        Account[] allChildren = new Account[] {};
            
            Set<Id> parentIds = new Set<Id>{accountId};
                
                Account[] children;
        
        do {
            
            children = [select Id, Name from Account where ParentId in :parentIds];
            
            allChildren.addAll(children);
            
            parentIds.clear();
            
            for (Account child : children) 
                
                parentIds.add(child.Id);
            
        } while (children.size() > 0);
        
        list<Account> Act = [select id, name from account where id in :allChildren];
        
        for(Account A : Act){
            
            system.debug('Child Accounts ' + a.name);  
            setactid.add(a.id);      
            
        }
        return setactid;
    }  
    

  }
Thanks
Sudhir
 
Leonardi KohLeonardi Koh
Hi Sudhir, your GetChildToParnet (parent? typo?) and GetParnetToChild methods are both doing query within the do loop

in GetChildToParnet
parent = [select Id,ParentId, Name from Account where Id in :parentIds];

and in GetParnetToChild
children = [select Id, Name from Account where ParentId in :parentIds];

these should be put outside the loop to avoid repeating queries that will quickly deplete the SOQL query limit our codes are given.
 
MaheemSamMaheemSam
Thanks Leonardi,

   Can you suggest how to make this query out of loop both the methods.

Thanks
Sudhir
Leonardi KohLeonardi Koh
Hi Sudhir,

It's actually not going to be that simple to replace it because if i understand this correctly, the methods are attempting to get all the records related to the account you provide by traversing the ParentID, and it would do this to however many depth at least until you run out of SOQL query limit that is.

this is a bit of a problem because there is no simple way to replace this traversing mechanism that i know of, not without having some sort of fixed depth.