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
Tom Blamire 24Tom Blamire 24 

Add Contact id from campaign member

As many people are aware that the Campaign/Campaign Member objectin Salesforce is very limited with regards to history. Having looked on the Salesforce community, SFDC offer a work around in the form of a custom ibject and trigger to help monitor any amendment made to the member record (added or deleted).
However, where it falls short is that if a member is deleted then it references only the member id in the new record and not the Contact or Lead id.

Below is the following trigger. How do i get to populate the Contact__c  or Lead__c field if the Contact/Lead is added or deleted?
 
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();
                                          c.User__c = userinfo.getuserId() ;
                                          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.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);
                                         }             
                           }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];
                                       c.User__c = userinfo.getuserId() ;
                                       System.debug('CampaignId:'+c.CampaignId__c);                                          
                                       c.HistoryDetails__c=changes[i];
                                       System.debug('CHistory:'+c.HistoryDetails__c);                                        
                                       ch.add(c);
                            } 
                            i++;     
                       }         
                }    
                insert ch;
     }

Thank you in advance for any help offered

Kind regards

Tom
Best Answer chosen by Tom Blamire 24
Veena Sundara-HeraguVeena Sundara-Heragu
If you have fields called Lead__c and Contact__c in your CampaignHistory__c object, all you have to do is:

c.Lead__c = cm.LeadId
c.Contact__c = cm.ContactId

If you just want to add  it in change history string, add the following to your string s:

If(cm.LeadId != null)
     s += ', Lead Id = ' + cm.LeadId ;
if(cm.ContactId != null)
    s += ', ContactI Id = '  + cm.ContactId

All Answers

Veena Sundara-HeraguVeena Sundara-Heragu
If you have fields called Lead__c and Contact__c in your CampaignHistory__c object, all you have to do is:

c.Lead__c = cm.LeadId
c.Contact__c = cm.ContactId

If you just want to add  it in change history string, add the following to your string s:

If(cm.LeadId != null)
     s += ', Lead Id = ' + cm.LeadId ;
if(cm.ContactId != null)
    s += ', ContactI Id = '  + cm.ContactId
This was selected as the best answer
Tom Blamire 24Tom Blamire 24
@Veena

That was all i needed! :)

thank you so much for your help, its been greatly appreciative!!!!!!!!