You need to sign in to do that
Don't have an account?

Help Bulkifying Trigger
I looked at the best practices page for bulkifying code here => http://wiki.developerforce.com/page/Apex_Code_Best_Practices I cannot seem to get it right because I still get the error of too many SQL Queries. Could someone please give me some direction as how to bulkify this trigger?
trigger UpdateProjectFromOpportunity on Opportunity (after update) {
for (opportunity theOpportunity : Trigger.new ) {
//Create an empty Tenrox Project List
List<tenrox__c> theTenroxProjects = new List<tenrox__c>();
For(tenrox__c theProject:[SELECT id, KI_Project_Coordinator_pre_sales__c FROM tenrox__c WHERE opportunity__c = :theOpportunity.id AND KI_CAD_Project_Status__c != 'Cancel' AND KI_CAD_Project_Status__c != 'Complete' limit 25 ])
{
if (theOpportunity.Project_Coordinator__c != null ) {
theProject.KI_Project_Coordinator_pre_sales__c = theOpportunity.Project_Coordinator__c;
}
theTenroxProjects.add(theProject);
} // End of For Loop for List
//Make sure there is something to update
if (!theTenroxProjects.isEmpty()) {
update theTenroxProjects;
} // End check for Records Processed
} // End of For Loop of Entire Trigger
} // End of Trigger
You never want to have a SOQL query in a for loop. This should help you out:
Good luck!
All Answers
You never want to have a SOQL query in a for loop. This should help you out:
Good luck!
Thank you VERY much! That was a really clear example. The trigger is now bulk worthy and I picked up on some new techniques. When rewritten it exposed several other issues that the existing test script was not catching either.
You should teach a class at Dreamforce with a real world example like that!