+ Start a Discussion
john2john2 

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;    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

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

Cory CowgillCory Cowgill

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;   

This was selected as the best answer
john2john2

Thanks for Quick reply,

 

but the code you given is not working for me.

 

Cory CowgillCory Cowgill

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