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
Aam MAam M 

Trgger to find campaigns associated to a contact.

I've a requirement related to campaigns.
I need to find campaigns associated to a contact. and if the contact has multiple campaigns, and for the 1st campaign that contact has Responded(camaign member status is responded) then that campaign name should be updated in the contact's field!
Can anyone explain me how can I update this, by writing trigger? or can I get a similar kind of trigger code
Ajay K DubediAjay K Dubedi
Hi Aam,
        
Here is a solution to your Problem.

            ------------Trigger-----------

trigger contactCampaignTrigger on Contact (after update) {

    if(checkRecursive.runOnce()){                                    \\ To avoid recursive trigger
        contactCampaignTriggerhandler.contactCamp(trigger.new);
   }
}            
            
            ---------Trigger_Handler-------
            
            
public class contactCampaignTriggerhandler {
    public static void contactCamp(List<Contact> lstCon){
        
        Set<Id> stConId = new  Set<Id>();
        for(Contact con : lstCon){
            stConId.add(con.Id);    
        }
        system.debug('set of ContactId>>>>>>>>>>>>>>>..'+ stConId);
        
        List<CampaignMember> lstCampMember = new List<CampaignMember>();
        lstCampMember = [Select Id,ContactId,CampaignId,Status From CampaignMember Where  ContactId =: stConId ORDER BY createdDate];
        system.debug('List of CampaignMember>>>>>>>>>>>>>>>..'+ lstCampMember);
        
        Set<Id> stCampId = new  Set<Id>();
        for(CampaignMember campMeb : lstCampMember){
            stCampId.add(campMeb.CampaignId);    
        }
        system.debug('set of CampaignId>>>>>>>>>>>>>>>..'+ stCampId);
        
        List<Campaign> lstCamp = new List<Campaign>();
        lstCamp = [Select Id,Name From campaign  Where Id IN:stCampId];
        system.debug('List of Campaign>>>>>>>>>>>>>>>..'+ lstCamp);
        
        map<Id,Campaign> mapOfCamp = new map<Id,Campaign>();
        for(Campaign camp : lstCamp){
            mapOfCamp.put(camp.Id, camp);
        }
        List<Contact> listToUpdate = new List<Contact>();
        for(Contact con: lstCon){
            Contact objcon = new Contact(id= con.Id);
            for(CampaignMember campMeb : lstCampMember){
                if(campMeb.Status == 'Responded'){
                    objcon.Campaign_Name__c = mapOfCamp.get(campMeb.CampaignId).Name;
                    listToUpdate.add(objcon);
                    break;
                }    
            }
        }
        if(listToUpdate.size() >0)
            update listToUpdate;
    }
}


                --------------checkRecursive_Apex Class------------------
                
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
        if(run){
            run=false;
            return true;
        }else{
            return run;
        }
    }
}

Please mark this as Best Answer if you find this solution helpful.
 
Thank You
Ajay Dubedi
Aam MAam M
Thank you so much It's working, Ajay.

But the thing is When I select the status RESPONDED, that time it should update the campaign name in contact's field. But now Everytime i'm updating the contact then only its updating! :( I want this to be updated on status of campaign member to be changed