+ Start a Discussion
Patrick Marks 2Patrick Marks 2 

Trigger for First/Most Recent Campaign on Contacts

Hey everyone-trying to write a simple trigger for populating a couple of custom lookup fields on Contacts. We have 2 custom fields called "First Campaign" and "Most Recent Campaign". I'm trying to get "First Campaign" populated with the first campaign the Contact was added to and "Most Recent Campaign" with the latest campaign they've been added to. Here's what I have so far, would love any help with this. Thanks in advance!
trigger FirstAndMostRecentCampaignOnContact on Contact (after insert, after update) {

    Set<Id> CampaignIds = new Set<Id>();
    for(Contact CON: Trigger.new) CampaignIds.add (CON.MostRecentCampaign__c);
    List<Campaign> c= new List<Campaign>([
        select 
        Id
        from Campaign 
        where Id = :CampaignIds ORDER BY CreatedDate DESC NULLS LAST LIMIT 1
    ]);
    Map<Id, List<Campaign>> cmMap = new Map<Id, List<Campaign>>();
    for (Campaign cm: c) {
        if (cmMap.containsKey(cm.Id)) {
            List<Campaign> x;
            x = cmMap.get(cm.Id);
            x.add(cm);
            cmMap.put(cm.Id, x);
        } else {
            List<Campaign> tmp = new List<Campaign>();
            tmp.add(cm);
            cmMap.put(cm.Id, tmp);
        }
    }
    List<Contact> CON1 = new List<Contact>(); 
    for(Contact newCon: Trigger.new){
        if (cmMap.containsKey(newCon.MostRecentCampaign__c)) {
            for (Campaign cm: cmMap.get(newCon.MostRecentCampaign__c)) {
                CON1.add(
                    New Contact(
                        id=newCon.id)                    
                );
            }
        }
    }
}

 
Best Answer chosen by Patrick Marks 2
nitin sharma 366nitin sharma 366
Hi,

I am not sure what exactly u are trying to do but below given code will populate lookup fields on the contact with values from the campaign object,However,this trigger is on the campainmember object and as soon as you add contact to the camapaignmember object trigger gets fired and it populates lookup field on the contact object.This trigger is far from perfect as I have put update statements within the for loop which is not good design but it does work
trigger UpdateCotaact on CampaignMember (before insert)
{


    set<id>contactids=new set<id>();
    contact cont;
    for(campaignmember cam:trigger.new)
    {
    
      contactids.add(cam.ContactId);  
        
    }
//Map<id, contact> contacts = new map<id, contact>([select id,Description,createddate,name from contact where id in:contactids ORDER BY CreatedDate DESC limit 1]);
Map<id, contact> contacts = new map<id, contact>([select id,First_Campaign__c,Description,createddate,name from contact where id in:contactids]);
    
    //Map<Id,Campaignmember>stat=new Map<Id,Campaignmember>([select id,contactid,campaignid from Campaignmember where contactid in:contactids]); 
    
//Map<id, contact> contact1 = new map<id, contact>([select id,Description,createddate,name from contact where id in:contactids order by createddate desc limit 1]);    
 
       
    for(Campaignmember bolly:trigger.new)
    {
        
       contact cont=contacts.get(bolly.ContactId);
       if(cont.First_Campaign__c==null)
       {
       cont.First_Campaign__c=bolly.CampaignId;
       update cont; 
       }
    }
    
Map<id,contact> contact1 = new map<id,contact>([select id,name from contact where id in:contactids]);    
     
    for(Campaignmember bolly1:trigger.new)
    {
        
       contact contra=contact1.get(bolly1.ContactId);
       
       
   
       
      
         
       contra.Most_Recent_Campaign__c=bolly1.CampaignId;
   
       update contra; 
       }
        
    }

All Answers

nitin sharma 366nitin sharma 366
Hi,

I am not sure what exactly u are trying to do but below given code will populate lookup fields on the contact with values from the campaign object,However,this trigger is on the campainmember object and as soon as you add contact to the camapaignmember object trigger gets fired and it populates lookup field on the contact object.This trigger is far from perfect as I have put update statements within the for loop which is not good design but it does work
trigger UpdateCotaact on CampaignMember (before insert)
{


    set<id>contactids=new set<id>();
    contact cont;
    for(campaignmember cam:trigger.new)
    {
    
      contactids.add(cam.ContactId);  
        
    }
//Map<id, contact> contacts = new map<id, contact>([select id,Description,createddate,name from contact where id in:contactids ORDER BY CreatedDate DESC limit 1]);
Map<id, contact> contacts = new map<id, contact>([select id,First_Campaign__c,Description,createddate,name from contact where id in:contactids]);
    
    //Map<Id,Campaignmember>stat=new Map<Id,Campaignmember>([select id,contactid,campaignid from Campaignmember where contactid in:contactids]); 
    
//Map<id, contact> contact1 = new map<id, contact>([select id,Description,createddate,name from contact where id in:contactids order by createddate desc limit 1]);    
 
       
    for(Campaignmember bolly:trigger.new)
    {
        
       contact cont=contacts.get(bolly.ContactId);
       if(cont.First_Campaign__c==null)
       {
       cont.First_Campaign__c=bolly.CampaignId;
       update cont; 
       }
    }
    
Map<id,contact> contact1 = new map<id,contact>([select id,name from contact where id in:contactids]);    
     
    for(Campaignmember bolly1:trigger.new)
    {
        
       contact contra=contact1.get(bolly1.ContactId);
       
       
   
       
      
         
       contra.Most_Recent_Campaign__c=bolly1.CampaignId;
   
       update contra; 
       }
        
    }
This was selected as the best answer
Patrick Marks 2Patrick Marks 2
Hi Nitin-this is perfect. I had the trigger set on the Contact object when it needed to be set on the Campaign Member object. Really appreciate the help!