0

I am trying to update the child record from the Parent using a trigger, this is what I am trying to accomplish: when the record is updated, the system should check for the field buyer_approved__c, if this is empty then the child field buyer_approved_flag__c (checkbox) should be false and vice versa. I don't know how to get the map value to make the comparison. I will appreciate any help.

This is what I have so far:

   trigger IPAapproved on Outbound_Sales_Order__c (after update) {

    Map<id,Outbound_Sales_Order__c > oso = new Map<ID,Outbound_Sales_Order__c >([select id, Buyer_Approved__c  from Outbound_Sales_Order__c  Where id IN :Trigger.new]);

    List<SO_Detail__c> osoDetailsToUpdate= [SELECT Id  FROM SO_Detail__c WHERE Outbound_Sales_Order__c IN :oso.KeySet()];

    for(SO_Detail__c rod : osoDetailsToUpdate)

  if(oso.get(2) != null){

     rod.Buyer_Approved_Flag__c = true;

      } else {

       rod.Buyer_Approved_Flag__c = false;

      }
      update osoDetailsToUpdate;
   }
1

1 Answer 1

4

Here is how I would write such a trigger:

trigger IPAapproved on Outbound_Sales_Order__c (after update) {

    // Only do work when the field has changed
    Map<Id, Outbound_Sales_Order__c> changed = new Map<Id, Outbound_Sales_Order__c>();
    for (Outbound_Sales_Order__c oso : Trigger.new) {
        Outbound_Sales_Order__c old = Trigger.oldMap.get(oso.Id);
        if (oso.Buyer_Approved__c != old.Buyer_Approved__c) {
            changed.put(oso.Id, oso);
        }
    }
    if (changed.size() > 0) {
        // Only update when the field has the wrong value
        List<SO_Detail__c> updates = new List<SO_Detail__c>();
        for (SO_Detail__c detail : [
                SELECT Id, Outbound_Sales_Order__c, Buyer_Approved_Flag__c
                FROM SO_Detail__c
                WHERE Outbound_Sales_Order__c IN :changed.keySet()
                ]) {
            Outbound_Sales_Order__c oso = changed.get(detail.Outbound_Sales_Order__c);
            Boolean requiredFlag = oso.Buyer_Approved__c != null;
            if (detail.Buyer_Approved_Flag__c != requiredFlag) {
                detail.Buyer_Approved_Flag__c = requiredFlag;
                updates.add(detail);
            }
        }
        update updates;
    }
}

The "trick" is to make the main loop run over the child object SO_Detail__c and have a map to lookup the parent Outbound_Sales_Order__c object from. That avoids awkward maps of lists.

3
  • Keith, thank you very much for your answer, I am getting this error: Variable does not exist: String at line 18 column 37. Please let me clarify, the oso.buyer_approved__c in the oso is a datetime field and the osoDetails. Buyer_approved_flag__c is a check box so when the date is placed in the oso (Parent ) the check box in the child(s) has to be checked in the other way around. Thank you in advance!
    – Carlos
    Nov 10, 2014 at 18:19
  • @Carlos See the change, to just a null check.
    – Keith C
    Nov 10, 2014 at 18:26
  • Keith, it is working.. thank you so much. Have a great day!!!
    – Carlos
    Nov 10, 2014 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .