You need to sign in to do that
Don't have an account?
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
}
All you queries are inside for loop. Move them out of for loop. Use collections like maps and lists for the same.
You can refer these links. Let me know if you need further details
http://techblog.appirio.com/2009/08/writing-bulk-triggers-for-salesforcecom.html
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bulk_idioms.htm
I have gone throgh all these links and other blogs but i really dont understand how to use key sets here.
Can you give me an example for this case.. I would appreciate. And Thanks for your quick response.
Hi,
It will helps to make perfect trigger.
Salesforce Best practice guide : http://wiki.developerforce.com/page/Best_Practices_for_Salesforce_to_Salesforce