You need to sign in to do that
Don't have an account?
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 =''; } } } }
try this.
[Do not use queries inside a loop]
All Answers
try this.
[Do not use queries inside a loop]
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 :)