Hoping someone can help me fix this. I understand that this error is because I'm trying to access a record that kicked off the trigger. I'm just not sure how to resolve it. Any help would be 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<String,CMRules__Email_Tracking_Summary__c> matchingHistMap = new
Map<String,CMRules__Email_Tracking_Summary__c>();
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.CMRules__Unique_Key__c, sum);
}
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();
list<string> key3 = h.CMRules__Unique_Key__c.split(':', 0);
if(uKey.containsKey(h.CMRules__Unique_Key__c)){
h.CMRules__Email_Tracking_Summary__c = matchingHistMap.get(h.CMRules__Unique_Key__c).Id;
historyFieldtoUpdate.add(h);
}else {
//create new summary record
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);
}
}
insert SummaryToInsert;
update historyFieldtoUpdate;
}