• Dustin Henshaw
  • NEWBIE
  • 0 Points
  • Member since 2018

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

I have recently started a new role heading up our tools here and I'm ready to deploy several new APEX features requested by my company.  However I've come across an Apex Test Failure from legacy code that my predecessor had created.  It's an important function for us as it is what calculates what sales territory the account should be a part of based on it's segment, territory and state/province, but we are getting the Too many SOQL queries on line 28 from the class and I can't for the life of me find out why, it's not part of a for loop from what I can see, i have even tried setting a limit on all the queries in the class to see if i can at least remove the error but no luck.  Am I missing something obvious? Any help with this would be greatly appreciated.

This is the class:
public class Account_TerritoryUpdate {
    public static void TerritoryUpdate(List<Account> newList, List<Account> oldList){
        
        Set<Id> accountIDs = new Set<Id>();
        Set<Id> parentIDs = new Set<Id>();
        
        for (Integer i=0; i<newList.size(); i++){
            // if the account is new, if Territory is Blank, or if Segment or State are changed.
            if(oldList==null || newList[i].Axonify_Sales_Territory__c==null
               || newList[i].Axonify_Segment_Model__c <> oldList[i].Axonify_Segment_Model__c
               || newList[i].BillingState <> oldList[i].BillingState){
                accountIDs.add(newList[i].Id);
            }
            if(newList[i].Ultimate_Parent__c<>null){
                parentIDs.add(newList[i].Ultimate_Parent__c);
            }
        }
        if (accountIDs.size()>0){
            List<Account> accountList = [Select Id, Axonify_Segment_Model__c, BillingState, Axonify_Sales_Territory__c, Ultimate_Parent__c from Account where ID in :accountIDs];
            Map<Id,Account> parentMap = new Map<Id,Account>([Select Id, Axonify_Sales_Territory__c from Account where ID in :parentIDs]);
            List<String> SegmentList = new List<String>();
            List<String> StateList = new List<String>();
            
            for(Account a:accountList){
                SegmentList.add(a.Axonify_Segment_Model__c);
                StateList.add(a.BillingState);
            }         
            List<Territory_Map__c> TerritoryMapList = [Select Id, Segment__c, State_Province__c, Territory__c from Territory_Map__c where Segment__c in :SegmentList or State_Province__c in :StateList]; /******************this is where the SOQL query error occurs**************/
            Map<String,String> tMap = new Map<String,String>();
            for (Territory_Map__c tm : TerritoryMapList){
                tMap.put(tm.Segment__c+tm.State_Province__c,tm.Territory__c);
            }
            List<Account> updateList = new List<Account>();
            for (Account a:accountList){
                if (parentMap.get(a.Ultimate_Parent__c)!=null && parentMap.get(a.Ultimate_Parent__c).Axonify_Sales_Territory__c<>a.Axonify_Sales_Territory__c){
                    a.Axonify_Sales_Territory__c = parentMap.get(a.Ultimate_Parent__c).Axonify_Sales_Territory__c;
                    updateList.add(a);
                }else if (a.Axonify_Sales_Territory__c <> tMap.get(a.Axonify_Segment_Model__c+a.BillingState)){
                    a.Axonify_Sales_Territory__c = tMap.get(a.Axonify_Segment_Model__c+a.BillingState);
                    updateList.add(a);
                }
            }
            
            update(updateList);        
        }
    }
}


Being called from this trigger:

trigger Account_Trigger on Account (after update, after insert, before delete) {
    
    if (Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert)){
        Account_TriggerHandler.ExcludeFromGainsight(Trigger.new,Trigger.old);
        Account_TerritoryUpdate.TerritoryUpdate(Trigger.new,Trigger.old); 
    }
    
    /******  Handle Child/Parent Account Stuff ******/
    if (Trigger.isBefore && Trigger.isDelete){
        // Prevent account from being deleted if there are any child accounts referencing it
        List<Id> parentIDs = new List<Id>();
        Map<Id,Account> parentMap = new Map<Id,Account>();
        for (Account a : Trigger.old){
            parentIDs.add(a.Id);
            parentMap.put(a.Id,a);
        }
        List<Account> childAccounts = [Select Id, ParentID, Ultimate_Parent__c from Account where ParentID in :parentIDs];
        if (childAccounts <> null){
            for(Account c : childAccounts){
                parentMap.get(c.ParentID).addError('An account cannot be deleted if it has child accounts.');
            }
        }
    }
    if (Trigger.isAfter && Trigger.isUpdate){
        // get accounts for which the parent has changed
        List<Id> accountIDs = new List<Id>();
        for (Integer i=0; i<Trigger.new.size(); i++){
            if (Trigger.new[i].ParentID <> Trigger.old[i].ParentID){
                accountIDs.add(Trigger.new[i].Id);
            }
        }
        if (accountIDs.size()>0){
            Account_ParentHandler.UpdateUltimateParent(accountIDs);
        }
    }else if (Trigger.isAfter && Trigger.isInsert){
        // get accounts which have a parent
        List<Id> accountIDs = new List<Id>();
        for (Account a : Trigger.new){
            if (a.ParentId <> null){
                accountIDs.add(a.Id);
            }
        }
        if (accountIDs.size()>0){
            Account_ParentHandler.UpdateUltimateParent(accountIDs);
        }
    }
    
}

Thanks in advance.