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
ynwaynwa 

Too many SOQL queries: 101

Hi All,

I am new to programming and this is my first apex trigger. I am writing a trigger to insert values from a custom object (ST__C) into account fields based on account country and postcode. There is no link between the two objects therefore the trigger needs loop through ST__c and find a match for the (string) variable account.billingcountry+account.billingpostcode.

 

I hit the limit SOQL queries issue if the batch size in dataloader is greater than 100.

 

I need help in making the code more efficient or setting a batch limit to 100 on the trigger (can we limit batch sizes triggers?

Thanks

 

trigger Assign_Sales_Rep on Account (before insert, before update) {
 // Grab Terriory information based on CountryPostcode
    map<String, String> map_CountryPostcode_to_SalesRepCS = new map<String, String>();
    map<String, String> map_CountryPostcode_to_SalesRepM = new map<String, String>();
    
    string AccCountryPostcode = ''; 

  for (Account sAccount : trigger.new)
  {
  	  	
        if (sAccount.BillingPostalCode == null || sAccount.BillingPostalCode == '' || sAccount.BillingPostalCode == ' ' || sAccount.BillingCountry == Null || sAccount.BillingCountry == '' || sAccount.BillingCountry == ' ')
           {
              //sAccount.ShippingPostalCode.AddError('Billing Postal Code OR Billing Country can not be null');
           }
           else 
			{
				  	
        AccCountryPostcode = sAccount.BillingCountry+sAccount.BillingPostalCode;
		System.debug('***********'+AccCountryPostcode);
    
    
		    for (Sale_Territory__c [] li_Sale_Territory: [ select CountryPostcode__c,
                                            Sales_Rep_CS__c,
                                            Sales_Rep_M__c
                                        from Sale_Territory__c
                                        where CountryPostcode__c = :AccCountryPostcode
                                        ]) 
    		{
        		for(Sale_Territory__c sSale_Territory : li_Sale_Territory)
        		{
            		map_CountryPostcode_to_SalesRepCS.put(sSale_Territory.CountryPostcode__c, sSale_Territory.Sales_Rep_CS__c);
            		map_CountryPostcode_to_SalesRepM.put(sSale_Territory.CountryPostcode__c, sSale_Territory.Sales_Rep_M__c);
		        }                               
		    }   
                if (map_CountryPostcode_to_SalesRepCS.containsKey(AccCountryPostcode))
                {
                    sAccount.Sales_Rep_CS__c = map_CountryPostcode_to_SalesRepCS.get(AccCountryPostcode);                   
                }
                else
                {
                    sAccount.Sales_Rep_CS__c ='FF';
                }
                if (map_CountryPostcode_to_SalesRepM.containsKey(AccCountryPostcode))
                {
                    sAccount.Sales_Rep_M__c = map_CountryPostcode_to_SalesRepM.get(AccCountryPostcode);                
                }
                else
                {
                    sAccount.Sales_Rep_M__c ='';
                }
           }           
    }
    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger Assign_Sales_Rep on Account (before insert, before update) 
{
	// Grab Terriory information based on CountryPostcode
    map<String, String> map_CountryPostcode_to_SalesRepCS = new map<String, String>();
    map<String, String> map_CountryPostcode_to_SalesRepM = new map<String, String>();
    
    string AccCountryPostcode = ''; 
	List<String> postalCodes = new List<String>();
	List<Account> accList = new List<Account>();
	
	for (Account acc : trigger.new)
	{
		if (acc.BillingPostalCode == null || acc.BillingPostalCode == '' || acc.BillingPostalCode == ' ' || acc.BillingCountry == Null || acc.BillingCountry == '' || acc.BillingCountry == ' ')
	    {
			//acc.ShippingPostalCode.AddError('Billing Postal Code OR Billing Country can not be null');
	    }
		else
		{
			accList.add(acc);
			AccCountryPostcode = acc.BillingCountry+acc.BillingPostalCode;
			postalCodes.add(AccCountryPostcode);
		}
  	
	}
	
	for(Sale_Territory__c st : [SELECT CountryPostcode__c, Sales_Rep_CS__c, Sales_Rep_M__c FROM Sale_Territory__c WHERE CountryPostcode__c IN :postalCodes])
	{
		map_CountryPostcode_to_SalesRepCS.put(sSale_Territory.CountryPostcode__c, sSale_Territory.Sales_Rep_CS__c);
        map_CountryPostcode_to_SalesRepM.put(sSale_Territory.CountryPostcode__c, sSale_Territory.Sales_Rep_M__c);
	}
	
	for(Account act : accList)
	{
		if (map_CountryPostcode_to_SalesRepCS.containsKey(AccCountryPostcode))
		{
			sAccount.Sales_Rep_CS__c = map_CountryPostcode_to_SalesRepCS.get(AccCountryPostcode);                   
		}
		else
		{
			sAccount.Sales_Rep_CS__c ='FF';
		}
		
		if (map_CountryPostcode_to_SalesRepM.containsKey(AccCountryPostcode))
		{
			sAccount.Sales_Rep_M__c = map_CountryPostcode_to_SalesRepM.get(AccCountryPostcode);                
		}
		else
		{
			sAccount.Sales_Rep_M__c ='';
		}
	}
}

 try this.

 

[Do not use queries inside a loop]

 

All Answers

Naidu PothiniNaidu Pothini
trigger Assign_Sales_Rep on Account (before insert, before update) 
{
	// Grab Terriory information based on CountryPostcode
    map<String, String> map_CountryPostcode_to_SalesRepCS = new map<String, String>();
    map<String, String> map_CountryPostcode_to_SalesRepM = new map<String, String>();
    
    string AccCountryPostcode = ''; 
	List<String> postalCodes = new List<String>();
	List<Account> accList = new List<Account>();
	
	for (Account acc : trigger.new)
	{
		if (acc.BillingPostalCode == null || acc.BillingPostalCode == '' || acc.BillingPostalCode == ' ' || acc.BillingCountry == Null || acc.BillingCountry == '' || acc.BillingCountry == ' ')
	    {
			//acc.ShippingPostalCode.AddError('Billing Postal Code OR Billing Country can not be null');
	    }
		else
		{
			accList.add(acc);
			AccCountryPostcode = acc.BillingCountry+acc.BillingPostalCode;
			postalCodes.add(AccCountryPostcode);
		}
  	
	}
	
	for(Sale_Territory__c st : [SELECT CountryPostcode__c, Sales_Rep_CS__c, Sales_Rep_M__c FROM Sale_Territory__c WHERE CountryPostcode__c IN :postalCodes])
	{
		map_CountryPostcode_to_SalesRepCS.put(sSale_Territory.CountryPostcode__c, sSale_Territory.Sales_Rep_CS__c);
        map_CountryPostcode_to_SalesRepM.put(sSale_Territory.CountryPostcode__c, sSale_Territory.Sales_Rep_M__c);
	}
	
	for(Account act : accList)
	{
		if (map_CountryPostcode_to_SalesRepCS.containsKey(AccCountryPostcode))
		{
			sAccount.Sales_Rep_CS__c = map_CountryPostcode_to_SalesRepCS.get(AccCountryPostcode);                   
		}
		else
		{
			sAccount.Sales_Rep_CS__c ='FF';
		}
		
		if (map_CountryPostcode_to_SalesRepM.containsKey(AccCountryPostcode))
		{
			sAccount.Sales_Rep_M__c = map_CountryPostcode_to_SalesRepM.get(AccCountryPostcode);                
		}
		else
		{
			sAccount.Sales_Rep_M__c ='';
		}
	}
}

 try this.

 

[Do not use queries inside a loop]

 

This was selected as the best answer
ynwaynwa

Thanks Naidu...I need to start using list, set and maps more.

Thanks again!!!...now I'm going to try and write the test class :)