You need to sign in to do that
Don't have an account?
Riptide
Message Edited by Riptide on 07-24-2008 11:18 AM
Make this a bulk trigger?
I have the following code that I have been asked to make into a bulk trigger. I'm new to Maps & Sets and I can't find any examples that meet my needs. Basically, the objects involved here are Offer & Listing. Offer is related to itself through the counter_offer_to field. If someone counters and offer I want to update the status of the offer being countered. If an offer is accepted, I want to update the status of the Listing. This code works fine but doesn't efficiently handle bulk updates. Any help is greatly appeciated.
trigger UpdateFirstOffer on Offer__c (after insert, after update) { for (Offer__c o : Trigger.new) { if (o.Offer_Type__c == 'First Offer') { } else { Offer__c c = [select Offer_Type__c from offer__c where Id = : o.Counter_Offer_To__c]; if (c.id != null) { if (o.Offer_Type__c == 'Counter Offer - Seller') { c.Offer_Status__c = 'Seller Countered'; update c; } if (o.Offer_Type__c == 'Counter Offer - Buyer') { c.Offer_Status__c = 'Buyer Countered'; update c; } } } if (o.Offer_Status__c == 'Sold - Seller Accepted Offer' || o.Offer_Status__c == 'Sold - Buyer Accepted Counter') { Listing__c l = [select Name from listing__c where Id = : o.Listing__c]; l.Listing_Status__c = 'Pending'; update l; } } }
Message Edited by Riptide on 07-24-2008 11:18 AM
Actually, if you have a one-to-one relationship between offer and counteroffer, you may not need a map.
What you need to do first is pull in all the offers that are related to the ones in your Trigger.new batch. The best way to do that is pull all the counter_offer_to IDs into a set, and then use a select to pull all the related offers at once.
Then I think all you need to do is through the Trigger.new batch like this:
and the values in the relatedOffers object will be changed.
Finally, you should place the Update statement outside the for-loop.
Good luck.
David
thanks
but that is the proper technique, and well explained too.