+ Start a Discussion
Tom Blamire 24Tom Blamire 24 

Code Tweek help

Hi,

I have this trigger which is fired when a campaign member is added, modified or deleted from a campaign to help with auditing purposes. What i am trying to do is to get it to populate either the Contact or Lead field (depending on member type) with the member who has been added, modified or deleted. Currently it only gives the campaign member id but this is useless if the person has been removed from a campaign as the campaign member id will no longer exist. Any help will be greatly appreciated
 
Trigger HistoryTrack on CampaignMember (after insert, before update, before delete)
 { List<CampaignHistory__c> ch= new List<CampaignHistory__c>(); List<CampaignMember> cmOld= Trigger.old;  List<String> changes  = new List<String>(); List<String> CampHisId  = new List<String>(); integer i=0; if(Trigger.isDelete){
            for(CampaignMember cm: Trigger.old ){     
                       String s;                            
                       s='Campaign Member id ' + cm.id +
                       '  has been deleted from campaign id:  '+ cm.campaignId + '   by user  '+ userinfo.getUserName();                                        
                       changes.add(s);
                       
                       CampHisId.add(cm.campaignId);
                       CampaignHistory__c c= new CampaignHistory__c();               
                       c.Name='History'+ DateTime.now();  
                       System.debug('CName:'+ c.Name);
                       c.User__c = userinfo.getuserId() ;
                       c.CampaignId__c = CampHisId[i];
                       System.debug('CampaignId:'+c.CampaignId__c);                              
                       c.HistoryDetails__c=changes[i];
                      System.debug('CHistory:'+c.HistoryDetails__c);         
                      ch.add(c);                              
                      i++;           
             } 
        }else {      
                     for(CampaignMember cm: Trigger.new ){  
                            String s;
                            if((Trigger.isUpdate)){
                                   if(cmOld[i].status!=cm.status){    
                                          s='on dated ' + DateTime.now() +                        
                                            ' status changed from ' + cmOld[i].status + ' to ' + cm.status +                        
                                            ' by user ' + userinfo.getUserName();
                                                                                       changes.add(s);
                                          CampHisId.add(cm.campaignId);                                                  
                                          CampaignHistory__c c= new CampaignHistory__c();                          
                                          c.Name='History'+DateTime.now();
                                          System.debug('CName:'+c.Name);
                                          c.CampaignId__c=CampHisId[i];                        
                                          System.debug('CampaignId:'+c.CampaignId__c);                                      
                                          c.HistoryDetails__c=changes[i];
                                          System.debug('CHistory:'+c.HistoryDetails__c);
                                          ch.add(c); 
                                      }else if(cmOld[i].campaignId!=cm.campaignId){                        
                                                s='Changed Campaign id from : '+ cmOld[i].campaignId + 'to :' + cm.campaignId +                        
                                                    ' by user '+ userinfo.getUserName();  
                                              changes.add(s);                        
                                          CampHisId.add(cm.campaignId);                                                            
                                          CampaignHistory__c c= new CampaignHistory__c();                           
                                          c.Name='History'+DateTime.now();
                                          System.debug('CName:'+c.Name);              
                                          c.CampaignId__c=CampHisId[i];                   
                                          System.debug('CampaignId:'+c.CampaignId__c);                                          
                                          c.HistoryDetails__c=changes[i];
                                          System.debug('CHistory:'+c.HistoryDetails__c);                         
                                          ch.add(c);
                                         }             
                           }else if(Trigger.isInsert){                                
                                       s='A new Campaign Member id : ' + cm.id + ' has been added to Campaign id :' + cm.campaignId +                                                          ' by user '+ userinfo.getUserName();
                                       changes.add(s);                    
                                       CampHisId.add(cm.campaignId);                    
                                       System.debug('s>>>'+s);                                                                    
                                       CampaignHistory__c c= new CampaignHistory__c();
                                       c.Name='History'+DateTime.now();                   
                                       System.debug('CName:'+c.Name);
                                       c.CampaignId__c=CampHisId[i];
                                       System.debug('CampaignId:'+c.CampaignId__c);                                          
                                       c.HistoryDetails__c=changes[i];
                                       System.debug('CHistory:'+c.HistoryDetails__c);                                        
                                       ch.add(c);
                            } 
                            i++;     
                       }         
                }    
                insert ch;
     }




Tom
Brenda S FinnBrenda S Finn
I need more information to understand what it is you are trying to do. You want to set the Contact or Lead field on the CampaignMember record depending upon which field has a value, is that correct? Or are you talking about wanting to set a value on your custom object CampaignHistory with this value from the CampaignMember? you are right - when deleting a CM, you cannot update it as the record is being removed from SF.
Tom Blamire 24Tom Blamire 24
Hi Brenda,

Thank you for replying. As you know, Campaign Members do not have a history related list meaning if you delete a campaign member it will not show who they were or who deleted them. As a result, I have created a custom object called Campaign Member History (CampaignHistory__c) which populates the following fields:

Campaign Name - The name of the campaoign that the member is/was associated with
History Details - Details of the Campaign Member, Campaign ID and User who has modified the campaign member

What i am trying to achieve is that if a Campaign Member is deleted/edited then populate the corresponding field with the id of the Contact/Lead not the Campaign Member Id.

I hope this makes sense?