+ Start a Discussion
kunakuna 

Too Many SOQL query:101

Hello,

I am using a soql query to retrieve records from Call Report.

I am able to execute this if i have less records but if i have more records that is fr about 200 Records i am getting an exception:
Too many SOQL queries: 101 .
Please help me out of this issue.


Here is my code :

 

 

trigger DisplayRltdCallRepOnAcc on Call_Report__c(after insert, after update,after delete){
    List<Call_Report__c> lastcallRltdCallRepOnAcc = new List<Call_Report__c>();
    String relatedToClientLastCall;
    List<Account> updateAccLastCall = new List<Account>();
    Set<Id> updatelastCallAccountIds = new set<Id>();
   
    List<Call_Report__c> nextCallRltdCallRepOnAcc = new List<Call_Report__c>();
    List<Account> accsToUpdateNextCall = new List<Account>();
    String relatedToClientNextCall;
    List<Account> updateAccNextCall = new List<Account>();
    Set<Id> updateNextCallAccountIds = new set<Id>();

   
    List<Call_Report__c> rltdToClientIds = new List<Call_Report__c>();
       
    if(trigger.isInsert || trigger.isUpdate){
    System.debug('Trigger.New values:::::: '+trigger.new);
        rltdToClientIds = trigger.new;
    }else if(trigger.isDelete){
     // Note -- there is no trigger.new in delete 
       rltdToClientIds = trigger.old;
     }
     System.debug('Related to Client IDS '+ rltdToClientIds.size());
     for(Call_Report__c calldates : rltdToClientIds){
    
        if(calldates.Related_to_Client__c != null){
            lastcallRltdCallRepOnAcc = [Select Call_Date__c,Related_to_Client__c From Call_Report__c c where Status__c='Complete'
                               and c.Type__c not in ('Attempt','Email') and Call_Date__c <= TODAY
                               and Related_to_Client__c!=null and
                               Related_to_Client__c =:calldates.Related_to_Client__c order by Call_Date__c desc];
        relatedToClientLastCall = calldates.Related_to_Client__c;
        }
        if(lastcallRltdCallRepOnAcc.size() > 0){
             System.debug('Last Call Date '+lastcallRltdCallRepOnAcc[0].Call_Date__c);
             Account acc = new Account(Id = lastcallRltdCallRepOnAcc[0].Related_to_Client__c,Last_Call_Date__c = lastcallRltdCallRepOnAcc[0].Call_Date__c);
             if(updatelastCallAccountIds.add(acc.Id)){
                 updateAccLastCall.add(acc);
             }
         }
         else{
             if(relatedToClientLastCall != null){
                 Account acc = new Account(Id = relatedToClientLastCall, Last_Call_Date__c = null);
                 if(updatelastCallAccountIds.add(acc.Id)){
                     updateAccLastCall.add(acc);
                 }
             }
         }
       }

}

 

 

 

 

 

 

Thanks in advance,
Ajit.

Best Answer chosen by Admin (Salesforce Developers) 
UVUV

Don't fire a query within a loop. More records Call_Report__c will have, more times query will execute.

 

All Answers

UVUV

Don't fire a query within a loop. More records Call_Report__c will have, more times query will execute.

 

This was selected as the best answer
Sandeep001Sandeep001

Please remove the SOQL query from the FOR loop and make use of Collections to hold the list of records.

kunakuna

Yes, I did the same.

Now its working fine.

 

Thank you :)

 

Regards

Ajit