3

I have a scenario where the three objects trigger have relatively same logic for beforeInsert and beforeUpdate. Even the fields of these objects that i use in trigger are same. this is my below code. i want to use sobject so that i dont mention the object name Bill_To__c in the below code. Instead I use sobject so that my other two objects Sold_To__c and Running_Account__c can use the same trigger helper method as below. Can anyone help?

public static void relateWCSSToAccount(Lis<Bill_To__c> wcssList,Map<Id,Bill_To__c> oldMap)  {
        List<String> guids = new List<String>();
        Map<String,Id> accountMap = new Map<String,Id>(); 
        for(Bill_To__c wcss:wcssList)
          {
           // In case of Update, Add GUID when trigger old map GUID is not equal to trigger new list GUID.
           if(oldMap!=null && oldMap.size()>0)
           {
            if(wcss.GUID__c!=(oldMap.get(wcss.Id).GUID__c))
              guids.add(wcss.GUID__c);
           }
           // Incase of insert; get the new GUIDs
           if(wcss.GUID__c!=null)
           {
            guids.add(wcss.GUID__c);
           }
          }

          if(guids.size()>0)
          {
            //Find matching accounts based on GUID
           for(Account a : [SELECT Id,GUID__c FROM Account WHERE GUID__c in:guids])
           {
            accountMap.put(a.GUID__c,a.Id);
           }
          }
          if(accountMap!=null && accountMap.keyset().size()>0)
          {
           // Iterate through Trigger.New list of WCSS
           for(WCSS_Bill_To__c wcssNew : wcssList)
           {
            if(wcssNew.GUID__c!=null)
            {
             if(accountMap.get(wcssNew.GUID__c)!=null)
              {
               // Update Account on each WCSS_Bill_To_c object.
                wcssNew.Account__c = accountMap.get(wcssNew.GUID__c);
              }
            }
           }
          }
       }

1 Answer 1

2

See below for a generic example of everything you should need to achieve what you are looking for. It shows how to pass in a List of SObjects, iterate over a List of SObjects, set and get on an SObject and also use the oldMap as well.

public static void relateSObjectToAccount(List<SObject> soList, Map<Id,SObject> oldMap) {
     for(SObject s : soList) {
         Id id = (Id) s.get('Id');
         String guid = (String) s.get('GUID__c');
         String oldGuid = (String) oldMap.get(id).get('GUID__c');
         if (guid != oldGuid) {
             s.put('FieldName__c', someValue);
         }
     }

     upsert soList;
}
3
  • this worked! just one more question.. I'm getting the GUIDs first by iterating Trigger.new list and creating account map based on GUIDS and again iterating through triggger.new list to see if guid is not null, to update account__c field.. I am iterating the new list twice once to get GUIDs and once to update.. is this acceptable?? or can I make it better?
    – user10930
    Commented Aug 11, 2014 at 15:31
  • It sounds like it could be made better but if there isn't too much happening in the loop then there isn't too much of an overhead in doing it.
    – BarCotter
    Commented Aug 11, 2014 at 16:12
  • yes makes sense.
    – user10930
    Commented Aug 11, 2014 at 19:25

You must log in to answer this question.

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