You need to sign in to do that
Don't have an account?
Bulkify Multi Object Trigger
I'm a total newbie to Apex and have diligently searched and searched through out the forums for a solution to my problem. I've managed to set up several triggers on my own but I'm facing difficulty with this one. Anyways, here's my trigger code:
trigger UpdateHotelAndRFP on Opportunity (before insert, before update) { for (Opportunity o : Trigger.new) { List<Lead> leads = [Select Id From Lead WHERE ConvertedOpportunityId = :o.Id]; List<RFP__c> rfp = [Select Id From RFP__c WHERE Lead__c IN :leads]; List<RFPResponse__c> rfps = [Select Id, Hotel__c, Contact__c, Response_Exceptions__c, Comp_Room_Policy__c, Hotel_Cancel_Policy__c, AdditionalFees__c From RFPResponse__c Where RFP_ID__c IN :rfp And Status__c = 'Awarded']; if (rfps.Size() > 0) { if(o.Hotel_Account_Name__c == null){ o.Hotel_Account_Name__c = rfps[0].Hotel__c; } if(o.Hotel_Contact__c == null) { o.Hotel_Contact__c = rfps[0].Contact__c; } if(o.RFP_Response__c == null){ o.RFP_Response__c = rfps[0].Id; o.Response_Exceptions__c = rfps[0].Response_Exceptions__c; o.Cancellation_Attrition_Policy__c = rfps[0].Comp_Room_Policy__c; o.Hotel_Cancel_Policy__c = rfps[0].Hotel_Cancel_Policy__c; o.Porterage_Fees_Additional_Fees__c = rfps[0].AdditionalFees__c; } } } }
Essentially a Lead has one child object, who also has a child object.
- Lead
- RFP__c
- RFPResponse__c
- RFP__c
When a Lead is converted I want to bring across the RFPResponse child object and perform a few updates to the Opportunity based on the contents of the RFPResponse object.
This trigger works perfectly, except that i cannot use it on bulk updates. I get the SOQL exception 21+ queries error message. Any help?
So here's what it took to bulkify my trigger..
All Answers
Have you looked at the Bulkify section on this page:
http://wiki.developerforce.com/index.php/Apex_Code_Best_Practices
You will also need to use SOQL relationship queries so you can pull the Lead related records along with the Lead records and work on the related records through the Lead records.
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm
You may also need to use Maps to map Opp IDs to Lead records(+their related records)
Here's an outline of what you need to do:
* use trigger.map.keyset to extract the opp IDs into a set.
* do a SOQL relationship query to get the Leads and related records. put the keyset in your where condition.
* possibly create a map of opp IDs to lead records.
* loop through your opps and perform your actions. Use map.get method to get the lead that corresponds to the opp in the loop.
David
So here's what it took to bulkify my trigger..