• SHRUTI HASILKAR 8
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hi

I am getting "System.LimitException: Apex heap size too large: 67116041" error. Please find my below piece of code.

public static void checkC4StageLA(List<Account> accountList, Map<id,Account> oldMap){
        Transient Integer count = 0;
        List<Opportunity> oppList = new List<Opportunity>();
        
        system.debug('accountList '+accountList); 
        List<Account> accList = new List<Account>();
        Map<ID, List<Opportunity>> oppMap = new Map<ID, List<Opportunity>>();
        
        oppList = [SELECT ID, Name, AccountID, stageName FROM Opportunity WHERE AccountID IN : accountList];
         
         system.debug('accountList '+accountList); 
         if(oppList.size() > 0){
            List<Opportunity> tempOppList = new List<Opportunity>();
            for(Opportunity opp: oppList){ //Reason behind to write Query in for loop is: To avoid heap size limit exception.
                if(oppMap.containsKey(opp.AccountID)){
                    tempOppList.addAll(oppMap.get(opp.AccountID));    
                }
                tempOppList.add(opp);
                oppMap.put(opp.AccountID, tempOppList);    
            }
            
          }
          
          List<Profile> PROFILE = [SELECT Id, Name FROM Profile WHERE Id=:userinfo.getProfileId() LIMIT 1];
          String MyProflieName = PROFILE[0].Name;
          system.debug('MyProflieName  '+MyProflieName );
          
          for(Account acc: accountList){
              if(oppMap.containsKey(acc.ID) && oppMap.get(acc.ID).size() > 0){
                  for(Opportunity opp: oppMap.get(acc.ID)){
                      if(opp.stageName == 'C4- Payment Disbursed'){
                          System.debug('ID '+opp.ID);
                          count++;    
                      }
                  }
              }    
              
              System.debug('count '+count );
          //    System.debug('oppMap.get(acc.ID).size() '+oppMap.get(acc.ID).size());
              if(oppMap.get(acc.ID) != null){
                  if(count == oppMap.get(acc.ID).size() && (MyProflieName == 'Marketing Officer' || MyProflieName == 'Branch Manager') && oppMap.get(acc.ID) != null  ){
                      System.debug('count '+count+' oppMap.get(acc.ID).size() ' +oppMap.get(acc.ID).size());
                      acc.addError('You are not having authority to chage account details.');    
                  }
              }
              
              count = 0;
          }
            
    }
-------------------------------------------------------------------------------------------------------------------------------------------
This method I am calling in Before Update of Account Trigger.

Please suggest me, how I can resolve this error.

Thanks in advance,
Shruti Hasilkar.