function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RiptideRiptide 

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
dmchengdmcheng
Here are some suggestions.  I haven't tested the code below.  I just recently figured out how to do maps myself, so there may be mistakes in this code.

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.

Code:
Set<Id> relatedOfferIDs = new Set<Id>();
Offer__c[] relatedOffers = new List<Offer__c>();

for (Offer__c o : Trigger.new) {
  relatedOfferIDs.add(o.counter_offer_to__c);
}

relatedOffers = [Select Offer_Type__c from offer__c where Id = : relatedOfferIDs];

 
Then I think all you need to do is through the Trigger.new batch like this:
Code:
for (Offer__c o : Trigger.new) {
  If (o.offerType == 'qwerty' {
    o.relatedOffers.Status_c = 'uiop';
    ;
;

and the values in the relatedOffers object will be changed.

Finally, you should place the Update statement outside the for-loop.


Good luck.
David


Ron HessRon Hess
David, a very clear explanation!
thanks
dmchengdmcheng
You mean the code was all correct too?  Woohoo!!  :smileyhappy:
Ron HessRon Hess
i didn't say that :smileyvery-happy:

but that is the proper technique, and well explained too.
RiptideRiptide
Hey David & Ron,
 
Thank you for the advice.  You pointed me in the right direction.  I tried David's code as written and got a foreign key error but from his code I was able to understand the set relationship and figure out how to add in a Map.  So, I'm posting the final, working code here so maybe someone else won't want to pull their hair out when they are told they need to handle bulk inserts.
 
Code:
trigger UpdateFirstOffer2 on Offer__c (after insert, after update) {

    Set<Id> relatedOfferIDs = new Set<Id>();
    Set<Id> relatedListingIDs = new Set<Id>();
    Offer__c[] relatedOffers2 = new List<Offer__c>();
    Offer__c oo;
    Listing__c[] relatedListings2 = new List<Listing__c>();
    Listing__c l;
    
    //Add all related offers to a List
    for (Offer__c o : Trigger.new) {
     //Only add records with a relationship established
     if (o.counter_offer_to__c != null) {
          relatedOfferIDs.add(o.counter_offer_to__c);
     }
    }
    
    //Create a map to associate the records being inserted with their related records
    Map<Id, Offer__c> relatedOffers = new Map<Id, Offer__c>(
        [Select Offer_Status__c from offer__c where Id in : relatedOfferIDs]);

    //Loop through the trigger results and update the related offer records
    for (Offer__c o : Trigger.new) {
      //Don't add records without a related record to the List
      if (o.counter_offer_to__c != null) {
          if (o.Offer_Type__c == 'Counter Offer - Seller') {
                //Assign the record from the map to a temporary variable
                oo = relatedOffers.get(o.counter_offer_to__c);
                oo.Offer_Status__c = 'Seller Countered';
          }   
          if (o.Offer_Type__c == 'Counter Offer - Buyer') {
                //Assign the record from the map to a temporary variable  
                oo = relatedOffers.get(o.counter_offer_to__c);
                oo.Offer_Status__c = 'Buyer Countered';
          }
          //Add the variable to the list
          relatedOffers2.add(oo);
      }
    }
    //Update the related records
    update relatedOffers2;
    
    //Add all related Listing records to a List
    for (Offer__c o : Trigger.new) {
     //Only add records with a relationship established
     if (o.listing__c != null) {
          relatedListingIDs.add(o.listing__c);
     }
    }
    
    //Create a map to associate the records being inserted with their related Listing records
    Map<Id, Listing__c> relatedListings = new Map<Id, Listing__c>(
        [Select Listing_Status__c from listing__c where Id in : relatedListingIDs]);
    
    //Loop through the trigger results and update the related Listing record
    for (Offer__c o : Trigger.new) {    
        if (o.listing__c != null) {
            l = relatedListings.get(o.listing__c);
            if (o.Offer_Status__c == 'Sold - Seller Accepted Offer' || o.Offer_Status__c == 'Sold - Buyer Accepted Counter') 
            {                     
                l.Listing_Status__c = 'Pending';
            }
        }
        //Add the variable to the list
        relatedListings2.add(l);
    }
    update relatedListings2;    
    }