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
Donald HurseyDonald Hursey 

Trigger to relate a record to another when a matching field is found

I'm trying to write my first trigger and not having much success. I have two objects email history (wbsendit__Campaign_Activity__c) and email summary (CMRules__Email_Tracking_Summary__c). email history looks up to email summary. The trigger should look to see if the email summary exists based on a field on both objects that is a combination of the campaign name, and the contact id (green0031U00000I0R12), or create the summary record.
I haven't been able to figure out how to relate the history to an existing summary, and then update the lookup field on the email history record with the ID of the found email summary record. I am able to create a summary record with the trigger below, but it isn't adding the lookup relationship from the email history that kicked off the trigger to the new summary.
Any help with this would be much appreciated!
 
trigger EmailSummaryCreate on wbsendit__Campaign_Activity__c (after insert, after update) {

//List of summary records that needs to be created 
List<CMRules__Email_Tracking_Summary__c> SummaryToInsert = new 
List<CMRules__Email_Tracking_Summary__c>();

//    set for only the campaign names in the current context
set<string> uKeyHistory = new set<string>();
for(wbsendit__Campaign_Activity__c hKey : trigger.new){
   if(hKey.CMRules__Unique_Key__c != null){
       uKeyHistory.add(hKey.CMRules__Unique_Key__c);
   }
}

Map<id,String> matchingHistMap= new Map<id,String>();
for(CMRules__Email_Tracking_Summary__c sum : [SELECT Id, 
CMRules__Unique_Key__c 
FROM CMRules__Email_Tracking_Summary__c 
where CMRules__Unique_Key__c IN :uKeyHistory]){
matchingHistMap.put(sum.Id, sum.CMRules__Unique_Key__c);
}  

List<wbsendit__Campaign_Activity__c> historyFieldtoUpdate = new 
List<wbsendit__Campaign_Activity__c>();



 // map of existing summaries records
 Map<string, Id> uKey = new Map<string, Id>();
 for(CMRules__Email_Tracking_Summary__c u : [SELECT Id, 
 CMRules__Unique_Key__c 
 FROM CMRules__Email_Tracking_Summary__c 
 where CMRules__Unique_Key__c IN :uKeyHistory]){
   uKey.put(u.CMRules__Unique_Key__c, u.Id); 
 } 

  for(wbsendit__Campaign_Activity__c h : trigger.new){

   boolean      key1 = uKey.containsKey(h.CMRules__Unique_Key__c);
   list<string> key2 = uKey.values();
   list<string> key4 = matchingHistMap.values();
   string[]     key3 = h.CMRules__Unique_Key__c.split(':', 0);       

   if(uKey.containsKey(h.CMRules__Unique_Key__c)){   

     //update CMRules__Email_Tracking_Summary__c lookup field with Id
        for(wbsendit__Campaign_Activity__c u : trigger.new){
            if(matchingHistMap.get(u.CMRules__Unique_Key__c) != null){
                  key3 = matchingHistMap.values();

 historyFieldtoUpdate.add(matchingHistMap.get(u.CMRules__Unique_Key__c));
                }
          }

       }else {             
           //create new summary record if one doesn't exist using the campaign name and contact Id as reference
           CMRules__Email_Tracking_Summary__c s = new CMRules__Email_Tracking_Summary__c();              
           s.CMRules__Campaign_Name__c = h.name;
           s.CMRules__Contact__c = h.wbsendit__Contact__c;

          SummaryToInsert.add(s);                        
       }
   }

  try {
   insert SummaryToInsert;
   //update historyFieldUpdate;

    } catch (system.DMLexception ex) {    
  }
 }