You need to sign in to do that
Don't have an account?
JN22
System.LimitException: Too many SOQL queries: 101
Hello,
I have the trigger below that fires on updates to my Account objects. The trigger works fine and tests fine, but when I try to make mass changes via the data loader I get the error message:
UpdateConsultantAcct: System.LimitException: Too many SOQL queries: 101
Can someone help me figure out why I am getting this error? Thanks,
trigger UpdateConsultantAcct on Account (after update){
List<Account> acct = new List<Account>();
Opportunity[] oppsToUpdate = new List<Opportunity>();
FOR(Account a : Trigger.new){
// Query Opportunity records realted to Account that is being created/edited
List<Opportunity> opps = [SELECT AccountId, IsWon, IsClosed, Consultant_Partner__c, Consultant_Type__c
FROM Opportunity
WHERE AccountId IN: Trigger.newMap.keySet()];
// Iterating through all the Opportunity records and updating them as per the conditions
for(Opportunity opps2 :opps){
// Update Consultant Partner field on Opportunity if Opp is Open and Consultant field changes at Account level
IF(opps2.IsWon == FALSE && opps2.IsClosed == FALSE && opps2.Consultant_Type__c == 'Primary Consultant' && trigger.NewMap.get(opps2.AccountId).Consultant_Partner_Primary__c != trigger.OldMap.get(opps2.AccountId).Consultant_Partner_Primary__c){
opps2.Consultant_Partner__c = a.Consultant_Partner_Primary__c;
oppsToUpdate.add(opps2);
}
ELSE IF(opps2.IsWon == FALSE && opps2.IsClosed == FALSE && opps2.Consultant_Type__c == 'Secondary Consultant' && trigger.NewMap.get(opps2.AccountId).Consultant_Partner_Secondary__c != trigger.OldMap.get(opps2.AccountId).Consultant_Partner_Secondary__c){
opps2.Consultant_Partner__c = a.Consultant_Partner_Secondary__c;
oppsToUpdate.add(opps2);
}
}
// Update all realted Opportunity records
//IF(opps.size() > 0)
// update opps;
}
update oppsToUpdate;
}
I have the trigger below that fires on updates to my Account objects. The trigger works fine and tests fine, but when I try to make mass changes via the data loader I get the error message:
UpdateConsultantAcct: System.LimitException: Too many SOQL queries: 101
Can someone help me figure out why I am getting this error? Thanks,
trigger UpdateConsultantAcct on Account (after update){
List<Account> acct = new List<Account>();
Opportunity[] oppsToUpdate = new List<Opportunity>();
FOR(Account a : Trigger.new){
// Query Opportunity records realted to Account that is being created/edited
List<Opportunity> opps = [SELECT AccountId, IsWon, IsClosed, Consultant_Partner__c, Consultant_Type__c
FROM Opportunity
WHERE AccountId IN: Trigger.newMap.keySet()];
// Iterating through all the Opportunity records and updating them as per the conditions
for(Opportunity opps2 :opps){
// Update Consultant Partner field on Opportunity if Opp is Open and Consultant field changes at Account level
IF(opps2.IsWon == FALSE && opps2.IsClosed == FALSE && opps2.Consultant_Type__c == 'Primary Consultant' && trigger.NewMap.get(opps2.AccountId).Consultant_Partner_Primary__c != trigger.OldMap.get(opps2.AccountId).Consultant_Partner_Primary__c){
opps2.Consultant_Partner__c = a.Consultant_Partner_Primary__c;
oppsToUpdate.add(opps2);
}
ELSE IF(opps2.IsWon == FALSE && opps2.IsClosed == FALSE && opps2.Consultant_Type__c == 'Secondary Consultant' && trigger.NewMap.get(opps2.AccountId).Consultant_Partner_Secondary__c != trigger.OldMap.get(opps2.AccountId).Consultant_Partner_Secondary__c){
opps2.Consultant_Partner__c = a.Consultant_Partner_Secondary__c;
oppsToUpdate.add(opps2);
}
}
// Update all realted Opportunity records
//IF(opps.size() > 0)
// update opps;
}
update oppsToUpdate;
}
Move this to above your FOR LOOP.
List<Opportunity> opps = [SELECT AccountId, IsWon, IsClosed, Consultant_Partner__c, Consultant_Type__c
FROM Opportunity
WHERE AccountId IN: Trigger.newMap.keySet()];
All Answers
Move this to above your FOR LOOP.
List<Opportunity> opps = [SELECT AccountId, IsWon, IsClosed, Consultant_Partner__c, Consultant_Type__c
FROM Opportunity
WHERE AccountId IN: Trigger.newMap.keySet()];