You need to sign in to do that
Don't have an account?
dnvansant
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{}}}
I would resist using DML in a loop.
All Answers
Possible error:
maybe you running it from page that define as 'ReadOnly="true"'
?
I would resist using DML in a loop.
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.
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