function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dev SFDev SF 

Too many SOQL queries: 101 Unable to insert multiple records

I am facing Too many SOQL queries: 101 error while updating records in two objects through trigger when record is inserted.
Tried to resolve this but unable to do so any help would be apprieciated. The code is below

 

trigger UpdateVAAccount on Coaching__c (after insert)
{
if (Trigger.isInsert)
{
 for(Coaching__c t:Trigger.new)
  {
   if(t.VA_ID__c != null)
   {
     if ([select id from Our_VA__c where id = :t.VA_ID__c].size() > 0 )
     {
     Our_VA__c o = [SELECT id, T_Recommendation_Date__c,T_Recommendation_Count__c, T_Recommended_Status__c, T_Recommendation_Client__c FROM Our_VA__c WHERE id =: t.VA_ID__c];
     //for multiple coaching create list of coachings that has this specific VA
     List<Coaching__c> coach = [SELECT VA_ID__c,id, T_Recommendation_Count__c,T_Recommendation_Date__c, T_Recommended_Status__c, T_Recommendation_Client__c FROM Coaching__c WHERE  VA_ID__c =: t.VA_ID__c];   
     //clean previous data in VA
     o.T_Recommendation_Date__c ='';
     o.T_Recommended_Status__c ='';
     o.T_Recommendation_Client__c ='';
     o.T_Recommendation_Count__c = 0;
     update(o);
     //Iterate all coachings and get their records to update in VA
     for(Coaching__c co : coach)
     {
     o.T_Recommendation_Date__c += co.T_Recommendation_Date__c;
     o.T_Recommended_Status__c += co.T_Recommended_Status__c;
     o.T_Recommendation_Client__c += co.T_Recommendation_Client__c;  
     o.T_Recommendation_Count__c = o.T_Recommendation_Count__c + co.T_Recommendation_Count__c;    
     } // for
     update(o);
     } // if
    } //if null  
    
    /*  For Account Update */
    
    
    if(t.Client_ID__c != null)
   {
     if ([select id from Account where id = :t.Client_ID__c].size() > 0 )
     {
     Account o = [SELECT id, T_Recommendation_Date__c,T_Recommendation_Count__c, T_Recommended_Status__c, T_Recommended_VA__c FROM Account WHERE id =: t.Client_ID__c];
     //for multiple coaching create list of coachings that has this specific VA
     List<Coaching__c> coach = [SELECT  Client_ID__c, id, T_Recommendation_Date__c,T_Recommendation_Count__c, T_Recommended_Status__c, T_Recommended_VA__c, T_Name_of_VA__c FROM Coaching__c WHERE Client_ID__c =: t.Client_ID__c];   
     //clean previous data in Account    
     o.T_Recommendation_Date__c ='';
     o.T_Recommended_Status__c ='';
     o.T_Recommended_VA__c ='';
     o.T_Name_of_VA__c ='';     
     o.T_Recommendation_Count__c = 0;
     update(o);
     //Iterate all coachings and get their records to update in Account
     for(Coaching__c co : coach)
     {
     o.T_Recommendation_Date__c += co.T_Recommendation_Date__c;
     o.T_Recommended_Status__c += co.T_Recommended_Status__c;
     o.T_Recommended_VA__c += co.T_Recommended_VA__c;  
     o.T_Name_of_VA__c += co.T_Name_of_VA__c;  
     o.T_Recommendation_Count__c = o.T_Recommendation_Count__c + co.T_Recommendation_Count__c;
     } // for
     update(o);
     } // if
    }  // if null 
    /*  End of For Account Update */
 } // main for
}//If Trigger Insert

}

Rakesh Aggarwal.ax1406Rakesh Aggarwal.ax1406

All you queries are inside for loop. Move them out of for loop. Use collections like maps and lists for the same. 

Dev SFDev SF

I have gone throgh all these links and other blogs but i really dont understand how to use key sets here.

Dev SFDev SF

Can you give me an example for this case.. I would appreciate. And Thanks for your quick response.

RajiiiRajiii