You need to sign in to do that
Don't have an account?
john2
Trigger error:Too many SOQL Queries
Hi ,
Iam getting this error, iam not sure how to avoid this...Any Help plz.....
System.LimitException: Too many SOQL queries: 21".
here is the thing i need to do is update primary unit field in account when is primary unit=true from units object.
trigger UpdatePrimaryUnit on Unit__c (after insert, after update) {
List <Account> acct= new List<Account>();
for(Unit__c ut : trigger.new){
system.debug('Entered for loop');
if(ut.Is_Primary_Unit__c==TRUE)
{
Account acc = [Select Name, Primary_Unit__c From Account Where Id=:ut.Account__c];
system.debug(acc.Id);
acc.Primary_Unit__c=ut.id;
acct.add(acc);
}
}
update acct;
}
You have a For loop which has a nested SOQL query. This is a common mistake with new developers on the platform.
Apex has Governor Limits which impose restrictions on the amount of resources, DML statements, etc you can make in your code.
You need to bulkify your code by using Maps and Lists.
Something like this:
List<String> accountIds = new List<String>();
for(Unit__c ut : trigger.new)
{
accountIds.add(ut.Account__c);
}
List<Account> accounts = [Select Id, Name, Primary_Unit__c From Account Where Id in :accountIds];
Map<String,Account> accountMap = new Map<String,Account>();
for(Account a : accounts)
{
accountMap.put(a.Id, a);
}
List <Account> acctUpdates = new List<Account>();
for(Unit__c ut : trigger.new){
system.debug('Entered for loop');
if(ut.Is_Primary_Unit__c==TRUE)
{
Account acc = accountMap.get(ut.Account__c);
system.debug(acc.Id);
acc.Primary_Unit__c=ut.id;
acctUpdates.add(acc);
}
}
update acct;
All Answers
You have a For loop which has a nested SOQL query. This is a common mistake with new developers on the platform.
Apex has Governor Limits which impose restrictions on the amount of resources, DML statements, etc you can make in your code.
You need to bulkify your code by using Maps and Lists.
Something like this:
List<String> accountIds = new List<String>();
for(Unit__c ut : trigger.new)
{
accountIds.add(ut.Account__c);
}
List<Account> accounts = [Select Id, Name, Primary_Unit__c From Account Where Id in :accountIds];
Map<String,Account> accountMap = new Map<String,Account>();
for(Account a : accounts)
{
accountMap.put(a.Id, a);
}
List <Account> acctUpdates = new List<Account>();
for(Unit__c ut : trigger.new){
system.debug('Entered for loop');
if(ut.Is_Primary_Unit__c==TRUE)
{
Account acc = accountMap.get(ut.Account__c);
system.debug(acc.Id);
acc.Primary_Unit__c=ut.id;
acctUpdates.add(acc);
}
}
update acct;
Thanks for Quick reply,
but the code you given is not working for me.
I just quickly typed out the code in the comment box so it my not be 100% syntax correct, but its what you need to do.
1. Create a list of the Account IDs from your Trigger.New object
2. Query ONE time for all the Accounts and store them in a List
3. Convert the List into a Map so you can easily retrieve the Accounts in your Trigger.new for loop without Querying again