• Chad Gill
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
an apex test keeps failing and is keeping us from being aable to create a package.  I believe it is due to a recursive loop.  The error i a get is the following:Error:

System.LimitException: RECON:Too many SOQL queries: 101

stack trace:

Trigger.RECON.Location: line 241, column 1


The code for the class trigger that creates the error is below:


trigger Location on Location__c (before insert,after insert, before update, after update, before delete, after delete) {

    

    // Hierarchy update trigger




    if(trigger.isBefore){

        

        list<Location__c> locationsToCheck = new list<Location__c>();

        

        list<Location__c> primaryJobSites = new list<Location__c>();

        

        

        if(trigger.isUpdate || trigger.isInsert){

            

            for(Location__c l:trigger.new){

                

                if(l.Job__c !=null && l.Primary_Job_Site__c){




                    primaryJobSites.add(l);

                }else if (l.Job__c ==null && l.Primary_Job_Site__c){




                    l.Primary_Job_Site__c=false;

                }




                if(l.id!=null){

                    Location__c lOld = trigger.oldmap.get(l.id);

                

                    if((lOld.Parent_Location__c != l.Parent_Location__c) || l.Hierarchy_Level__c==null){

                        locationsToCheck.add(l);

                    }

                }else{

                    locationsToCheck.add(l);

                }

            }

            if(DeepClone.getExecuteLocationHierarchyLogic()){

                if(!locationsToCheck.isEmpty()) HierarchyHelper.checkHierarchyOnUpdate(locationsToCheck);

            }

            if(!primaryJobSites.isempty()) PrimaryJobSite.checkPrimaryJobSites (primaryJobSites); 

        }   

            

        if(trigger.isDelete){

            Set<String> locId = new Set<String>();

            for(Location__c l : trigger.old){

                locationsToCheck.add(l);

                locId.add(l.id);

            }   

            if(!locationsToCheck.isEmpty()) HierarchyHelper.getChildrenWhenDeleted(locationsToCheck);

            if(!Test.isRunningtest())

                DeepDeletion.beforeDeletionLocation(locId);

        }

    }

    

    if(trigger.isafter){

        // Hierarchy Id  and Level Assignment

        set<id> locationsToUpdate = new set<id>();

        

        if(trigger.isUpdate ){

            List<RECON__Location_Change_Log__c> lsLocChangeLog = new List<RECON__Location_Change_Log__c>();

            locationsToUpdate = new set<id>();

            for(Location__c l : trigger.new){

                Location__c lOld = trigger.oldmap.get(l.id);

                if(lOld.RECON__Complete__c  != l.RECON__Complete__c ) {

                    RECON__Location_Change_Log__c locChLog = new RECON__Location_Change_Log__c();

                    locChLog.RECON__Complete__c = l.RECON__Complete__c;

                    locChLog.RECON__complete_Old__c = lOld.RECON__Complete__c;

                    locChLog.RECON__Location__c = l.id;

                    lsLocChangeLog.add(locChLog);

                }

                if((lOld.Parent_Location__c != l.Parent_Location__c) || l.Hierarchy_Id__c == null){

                    system.debug('***adding location Id to update set: ' + l.Id);

                    locationsToUpdate.add(l.id);

                }

            }

            if(lsLocChangeLog.size() > 0) {

                insert lsLocChangeLog;

            }

            System.debug('***locationsToUpdate set: ');

            system.debug(locationsToUpdate);

            

            if(HierarchyHelper.getExecuteLocationHierarchyIdLogic()){

                HierarchyHelper.updateHierarchyIds(locationsToUpdate);

            }

            if(DeepClone.getExecuteLocationHierarchyLogic()){

                HierarchyHelper.updateHierarchyItems(HierarchyHelper.getObjNameToHierarchyCalcIds());

            }

        }

        if(trigger.isInsert){




            locationsToUpdate = new set<id>();

            for(Location__c l : trigger.new){

                locationsToUpdate.add(l.id);

            }

            if(HierarchyHelper.getExecuteLocationHierarchyIdLogic()){

                if(!Test.isRunningtest())

                    HierarchyHelper.updateHierarchyIds(locationsToUpdate);

            }

            if(DeepClone.getExecuteLocationHierarchyLogic()){

                HierarchyHelper.updateHierarchyItems(HierarchyHelper.getObjNameToHierarchyCalcIds());

            }

        }

            

        if(trigger.isDelete){

            if(HierarchyHelper.getObjNameToHierarchyCalcIds()!=null){

                HierarchyHelper.updateChildrenAfterDeletion(HierarchyHelper.getObjNameToHierarchyCalcIds());

            }

            if(!Test.isRunningtest())

                DeepDeletion.afterDeletionLocation();

        }

    }

    

    // Completion % trigger

    

    if(trigger.isBefore && CompletionHelper.getExecuteLocationCompletionLogic()){

    

        if(trigger.isUpdate){

        

            for(Location__c l : trigger.new){

                

                Location__c lOld = trigger.oldMap.get(l.id);

                

                if(l.Complete__c != lOld.Complete__c){

                    

                CompletionHelper.addLocationId(l.Parent_Location__c);

            

                }

            }       

        }

    }

        

    if(trigger.isAfter){

        if(CompletionHelper.getExecuteLocationCompletionLogic()){

            if(trigger.isUpdate || trigger.isInsert){

                if(CompletionHelper.getLocationsToUpdate() != null){

                        CompletionHelper.updateLocationTree();

                }

            }

        }

    }







    // Geocode trigger




    if(trigger.isBefore || trigger.isAfter){




        set<id> locationIds = new set<id>();




        if(trigger.isUpdate){




            for(Location__c l:trigger.new){

                

                Location__c lOld = trigger.oldmap.get(l.id);




                if (l.Street__c != lOld.Street__c || l.City__c != lOld.City__c || l.State__c != lOld.State__c || l.Zip_Code__c != lOld.Zip_Code__c){

                    locationIds.add(l.id);

                }

            }

        }

        if(trigger.isInsert && trigger.isAFter){

            for(Location__c l : trigger.new){

                locationIds.add(l.id);

            }

        }




        if(locationIds.size()>0){

            LocationGeocode.getGeocodes(locationIds);

        }

        

    } 

    

    if(trigger.isBefore) {

        List<RECON__Location__c> ls;

        if(trigger.isDelete) {

            ls = trigger.old;

        }else {

            ls =trigger.new;

        }

        List<String> jId = new List<String>();

        for(RECON__Location__c jp : ls) {

            jId.add(jp.RECON__Job__c);

        }

        if(!Test.isRunningtest())

            DeepDeletion.putJobId(jId);

    }

    if(trigger.isAfter){

        if(!Test.isRunningtest()){

            Map<String,List<RECON__Location__c>> map_Loc_JP = new Map<String,List<RECON__Location__c>>();

            Map<String, Decimal> calculateHour = new Map<String, Decimal>();

            List<RECON__Location__c> ls = [Select Id,RECON__Estimated_Man_Hour__c,RECON__Job__c From RECON__Location__c where RECON__Job__c In :DeepDeletion.getJobId()];

            for(RECON__Location__c jp : ls) {

                if(jp.RECON__Job__c != null) {

                    if(!map_Loc_JP.containsKey(jp.RECON__Job__c)){

                        map_Loc_JP.put(jp.RECON__Job__c,new List<RECON__Location__c>());

                        map_Loc_JP.get(jp.RECON__Job__c).add(jp);

                        if(jp.RECON__Estimated_Man_Hour__c == null){

                            jp.RECON__Estimated_Man_Hour__c =0;

                        }

                        calculateHour.put(jp.RECON__Job__c, jp.RECON__Estimated_Man_Hour__c);

                    } else {

                        Decimal val= jp.RECON__Estimated_Man_Hour__c;

                        if(val != null){

                        system.debug('Manish : Debug '+val+' j p :' +calculateHour+'Loc Id :'+jp.RECON__Job__c);

                            val = val+calculateHour.get(jp.RECON__Job__c);}

                        else

                            val = calculateHour.get(jp.RECON__Job__c);

                        calculateHour.put(jp.RECON__Job__c, val);

                        map_Loc_JP.get(jp.RECON__Job__c).add(jp);

                    }

                }

            }

            Map<String, RECON__Job__c> map_Loc =new Map<String, RECON__Job__c>([Select Id, RECON__Estimated_Man_Hour__c from RECON__Job__c where id In :DeepDeletion.getJobId()]);

            for(String loc : map_Loc.keySet()) {

                if(calculateHour.get(loc) != null) {

                    map_Loc.get(loc).RECON__Estimated_Man_Hour__c = calculateHour.get(loc);

                } else {

                    map_Loc.get(loc).RECON__Estimated_Man_Hour__c =0.00;

                }    

                

            }

            system.debug('@manish trigger : '+map_Loc);

            system.debug('@manish calculateHour : '+calculateHour);

            system.debug('@manish map_Loc_JP : '+map_Loc_JP);

            if(map_Loc.size() > 0)

                update map_Loc.values();

        }

    }  

    //added by sanjeev

    //set<Id> locationId = new set<Id>();   

    if(Trigger.isAfter){

        set<Id> locationId = new set<Id>();

        if(Trigger.isInsert || Trigger.isUpdate){

            for(RECON__Location__c pLoc : Trigger.new){

                locationId.add(pLoc.RECON__Parent_Location__c);

                //locationId.add(pLoc.id);

            }

        }else

        if(Trigger.isDelete){

            for(RECON__Location__c pLoc : Trigger.old){

                //locationId.add(pLoc.id);

                locationId.add(pLoc.RECON__Parent_Location__c);

            }

        }

        List<RECON__Location__c> locationList = [select RECON__Estimated_Man_Hour__c, (select RECON__Estimated_Man_Hour__c from Locations__r) from RECON__Location__c where id IN : locationId];

        if(locationList.size() > 0){

            for(RECON__Location__c loc : locationList){

                //loc.RECON__Estimated_Man_Hour__c = 0;

                List<RECON__Location__c> locChilds = loc.Locations__r;

                if(locChilds.size() > 0){

                    for(RECON__Location__c locChild : locChilds){

                        loc.RECON__Estimated_Man_Hour__c += locChild.RECON__Estimated_Man_Hour__c;

                    }

                }

            }

            update locationList;

        }

    } 

    //till here

}




Is there something simple I am missing?  Thanks for your help.