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
dnvansantdnvansant 

Can anyone see why this code causes too many queries: 101 error?

It only has 1 query in a map which is not in a loop. Does "get" count as a query?

 

FYI, this works when updating Accounts in batches of 20 but not in batches of 50 or more.

 

 

trigger Territory on Account (after insert, after update) {

List<Account> ac = new List<Account>();
public list<string> name = new List<string>();

for(integer j=0; j < trigger.new.size();j++){
name.add(trigger.new[j].territory_OEM__c);  
name.add(trigger.new[j].territory_Direct__c); }

Map<String,String> territory = new Map<String,String>();
for (Territory__c t :[SELECT name, owner__c from Territory__c where name in :name])
{territory.put(t.name, t.Owner__c);}

for(integer i=0; i < trigger.new.size();i++){
If(
trigger.isinsert || (trigger.isupdate

&& (trigger.new[i].Temp__c == 62 && trigger.old[i].Temp__c != 62 ||
trigger.new[i].billingcountry != trigger.old[i].billingcountry ||
trigger.new[i].billingstate != trigger.old[i].billingstate ||
trigger.new[i].billingpostalcode != trigger.old[i].billingpostalcode ||
trigger.new[i].company_size__c != trigger.old[i].company_size__c
))

){
if(Territory.containsKey(trigger.new[i].Territory_OEM__c) && Territory.containsKey(trigger.new[i].Territory_Direct__c)){
ac.add(new account(
ID = trigger.new[i].ID,
territory_owner_OEM__c = territory.get(trigger.new[i].Territory_OEM__c),
territory_owner_direct__c = territory.get(trigger.new[i].Territory_Direct__c)
));else{}
}

if(ac.size() > 0){update ac;}else{}

}else{}}}

 

Best Answer chosen by Admin (Salesforce Developers) 
admintrmpadmintrmp
If my eyes are serving me correctly, I believe you are running DML on accounts within a loop. If you have a trigger in accounts or have a package that has a trigger on accounts, this could cause problems.

I would resist using DML in a loop.

All Answers

liron169liron169

Possible error:

maybe you running it from page that define as 'ReadOnly="true"'

?

admintrmpadmintrmp
If my eyes are serving me correctly, I believe you are running DML on accounts within a loop. If you have a trigger in accounts or have a package that has a trigger on accounts, this could cause problems.

I would resist using DML in a loop.
This was selected as the best answer
Eugene NeimanEugene Neiman

Is it possible you are in a loop?  This is an account trigger.  Based on territories found, you are inserting new accounts, which will fire this very after insert trigger.

dnvansantdnvansant

Thank you, my DML was in a loop. I know better but I misplaced my brackets, thanks for pointing it out. Everything is working now.

 

FYI, for others who may look at this in the future, Eugene is right, sometimes other triggers combine to exceed limits, but this was not my case this time.

 

-Derek