0

Hope all are doing good and safe!

I'm trying to executing the below code but getting too many SOQL queries exception. Kindy please suggest how can I avoid this error.

public void territoryBrickCodePopulation(List<Account> accList){
    
    String marketValue = system.label.BrickToTerritory_Market;
    Set<String> zipcodes=new Set<String>(); 
    Set<String> marketSet = new Set<String>();
    Set<String> zipMarketKey =new Set<String>();
      
    List<ZTS_GL_BrickToTerr__c> lstBrick = [select Id,Name,ZTS_EU_Market__c,ZTS_EU_BrickName__c,ZTS_EU_Territory__c from ZTS_GL_BrickToTerr__c];
     
    for(Account acc:accList){        
        
        for(ZTS_GL_BrickToTerr__c brick : lstBrick){                       
        
            if (marketValue != null && marketValue.contains(brick.ZTS_EU_Market__c)){
                          
                if(!String.isBlank(acc.ZTS_EU_Zip_Postal_Code__c) && acc.ZTS_EU_Market__c != ZTS_EU_GlobalConstants.MARKET_VAL && acc.ZTS_EU_Market__c != ZTS_EU_GlobalConstants.UK_MARKET_VAL){
                    zipcodes.add(acc.ZTS_EU_Zip_Postal_Code__c);
                }else if(acc.ZTS_EU_Market__c == ZTS_EU_GlobalConstants.MARKET_VAL){
                    zipcodes.add(acc.ZTS_EU_State_County_Value__c);
                }else if(!String.isBlank(acc.ZTS_EU_Zip_Postal_Code__c) && acc.ZTS_EU_Market__c == ZTS_EU_GlobalConstants.UK_MARKET_VAL){
                    zipcodes.add(acc.ZTS_EU_UK_PC4_from_Postalcode__c);
                }
                marketSet.add(acc.ZTS_EU_Market__c);
       }
     }
   } 

1 Answer 1

3

This is one of the governor limits of the platform.

In a single apex context your queries can return a max of 50000 records else you will hit the limit as the error suggests.

Consider first adding sufficient filter to your query, if you cannot then you have to move your logic into a batch apex that can process records in chunks and provides ability to query more than 50K rows via query locator in the start method.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .