• Dev SF
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

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

}

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

}