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
Vasu_vVasu_v 

Update contact from campaign member

Hi All,

I am trying to update contact field value from campaign member when ever campaign member (CURD) operation.
Details are Like we have a custom button in opportunity it will display list of contacts and parent and child campaigns.Like below screen.
So when we ever we click subscribe get the value of parent campaign and update in contact, I have written code but its not working as expected can any one suggest or help me where i am doing wrong.

User-added image

Apex Code.
/*
* This is handler class to update contact ,Entitile doc group from parent campain 
*/
public class ContactEntitledDocGroup1Test 
{
    public static void getContactServices(Set<Id> conIds,set<Id> campaignmemberId,set<Id> childcampaignId)
    {
        List<CampaignMember> camMemberList = new List<CampaignMember>();
        List<CampaignMember> ListcamMember = new List<CampaignMember>();
        List<CampaignMember> ListAllcamMember = new List<CampaignMember>();
        List<Campaign> childCamList = new List<Campaign>();
        List<Campaign> parentCamList = new List<Campaign>();
        List<Contact> contactList =new List<Contact>();
         List<Contact> selectedcontactList =new List<Contact>();
        Map<id,Contact>  UpdateCon=new  Map<id,Contact> ();
        set<id> parentId = new set<id>();
        set<id> campparentId = new set<id>();
        set<id> childId = new set<id>();
        set<id> childcampId = new set<id>();
        set<id> contactIds = new set<id>();
        map<id,set<id>> conCampMap= new map<id,set<id>>();
        map<id,map<id,set<id>>> campaignMap = new map<id,map<id,set<id>>>();
        Map<id,Map<id,Map<set<Id>,Set<ID>>>> allids=new Map<id,Map<id,Map<set<Id>,Set<ID>>>>();
        Map<set<Id>,Set<ID>> cmpcontactid=new Map<set<Id>,Set<ID>>();
        Set<Id> cmmid=new Set<Id>();
        Map<id,Map<set<Id>,Set<ID>>> childcmpcmmcontactid=new Map<id,Map<set<Id>,Set<ID>>>();
        Set<String> rixmlSet = new Set<String>();
         Set<Id> allChildcmpainIds=new Set<Id>();
        
         Set<Id> selectedcontactids=new Set<Id>();
        Map<Id,Set<Id>> contactcampaignId=new Map<Id,Set<Id>>();
        
         system.debug('************* campaignmemberId'+campaignmemberId);
         
         Map<Id,Set<Id>> parentchildmap=new Map<id,Set<Id>>();
        if(campaignmemberId != null && !campaignmemberId.isEmpty())
        {
            camMemberList = [Select id,CampaignId,Campaign.ParentId,ContactId,Paid_Service__c from campaignmember where id=:campaignmemberId and paid_service__c =true];
        }
        system.debug('************* camMemberList '+camMemberList );
        for(CampaignMember cam : camMemberList)
        {
            if(cam.ContactId != null && cam.Campaign.ParentId != null)
            {
                set<id> campnId = new set<id>();
                parentId.add(cam.Campaign.ParentId);
                childId.add(cam.CampaignId);
                contactIds.add(cam.ContactId);
                conCampMap.put(cam.ContactId, campnId);
                if(conCampMap.containsKey(cam.ContactId))
                {
                    conCampMap.get(cam.ContactId).add(cam.CampaignId);
                }
            }
        }
        System.debug('>>>>>>>>>>>>>>>>>>>>>>>>>'+conCampMap);
        if(!parentId.isEmpty() && parentId != null)
        {
            parentCamList = [Select id,RIXML_Code__c,Service_Reports__c from campaign where id =:parentId and RIXML_Code__c != null];
            for(Campaign par:parentCamList)
            {
            campparentId.add(par.id);
            
            }
        }
         System.debug('>>>>>>>>>>>>>>>>>>>>>>>>> parentCamList '+parentCamList );
        if(!childId.isEmpty() && childId != null)
        {
            childCamList = [Select id,ParentId,parent.RIXML_Code__c,parent.Service_Reports__c from campaign where parentid =:parentId];
        }
        if(childCamList !=null)
        {
            
      System.debug(' childCamList'+childCamList);
        for(campaign cms:childCamList)
        {
            if(cms.parentID !=null && cms.Parent.Rixml_Code__c !=null)
            {
                allChildcmpainIds.add(cms.Id);
            }
        }
             System.debug(' allChildcmpainIds'+allChildcmpainIds);
            if(allChildcmpainIds !=null)
            {
                ListAllcamMember=[Select id,CampaignId,Campaign.ParentId,ContactId,Paid_Service__c from campaignmember where Campaignid=:allChildcmpainIds and paid_service__c =true];
            }
        }
        
        if(!contactIds.isEmpty() && contactIds != null)
        {
            contactList = [select id,Entitled_Document_Groups_Last_Updated__c,Entitled_Document_Groups__c from contact where id =:contactIds];
        }
         System.debug(' ListAllcamMember'+ListAllcamMember);
        System.debug('>>>>>>>>>>>>>>>>>>>>>>>>> childCamList '+childCamList );
      
        for(contact con : contactList)
        {
        for(Campaign cm: childCamList)
        {
            for(CampaignMember cam : ListAllcamMember)
            {
                set<id> campnId = new set<id>();
                 
                    conCampMap.put(cam.ContactId, campnId);
                     System.debug('>>>>>>>>>>>>>>>>>>>>>>>>> conCampMap'+conCampMap);
                    if(conCampMap.containsKey(cam.ContactId))
                    {
                        conCampMap.get(cam.ContactId).add(cam.CampaignId);
                        System.debug('>>>>>>>>>>>>>>>>>>>>>>>>> conCampMap'+conCampMap);
                    }
                     
                 
                    
                if(cm.ParentId != null)
                {
               // parentchildmap.put(cm.ParentId,campnId);
               /* if(!parentchildmap.containsKey(cm.ParentId))
                {
                parentchildmap.put(cm.ParentId,new set<id>());
                }
                 parentchildmap.get(cm.ParentId).add(cm.id); */
                 if(con.id == cam.ContactId && cam.campaignid==cm.id)
                {        
                    if(!contactcampaignId.containsKey(con.Id))
                    {
                          contactcampaignId.put(con.Id,new set<id>());
                    }
                     contactcampaignId.get(con.Id).add(cam.campaignid);
                    
                      //  contactcampaignId.put(con.Id,allChildcmpainIds );
                 if(!parentchildmap.containsKey(cam.Campaign.ParentId))
                {
                parentchildmap.put(cam.Campaign.ParentId,new set<id>());
                    selectedcontactids.add(cam.contactID);
                    
                }
                 parentchildmap.get(cam.campaign.ParentId).add(cam.campaignid);
                 }
                    
                    childcampId.add(cm.id);
                    campaignMap.put(cm.ParentId,conCampMap);
                }
            }
        }
        }
         if(!selectedcontactids.isEmpty() && selectedcontactids != null)
        {
            selectedcontactList = [select id,Entitled_Document_Groups_Last_Updated__c,Entitled_Document_Groups__c from contact where id =:selectedcontactids];
        }
         System.debug(' parentchildmap>>>>>>>>>>>>>>>>>>>>>>>>> selectedcontactList'+selectedcontactList);
          System.debug(' parentchildmap>>>>>>>>>>>>>>>>>>>>>>>>> parentchildmap'+parentchildmap);
          System.debug('>>>>>>>>>>>>>>>>>>>>>>>>> campaignMap'+campaignMap);
        System.debug('>>>>>>>>>>>>>>>>>>>>>>>>> childcampId'+childcampId); 
        for(campaign cmps:parentCamList)
        {
        for(contact con:selectedcontactList)
        {
            for(id conid:contactcampaignId.keyset())
            {
                
           
        for(id parent:parentchildmap.keyset())
        {
        integer mapValue = Integer.ValueOf(parentchildmap.get(parent).Size());
        //set<id>str= parentchildmap.get(parent);
        System.Debug('%%%%%%%%%%%%%%%%%%% '+mapValue);
        System.Debug('%%%%%%%%%%%%%parent%%%%%% '+parent);
        System.Debug('%%%%%%%%%%%%%parent%%%%%% '+cmps.id);
         System.Debug('%%%%%%%%%%%%%parent%%%%%% '+cmps.Service_Reports__c);
          System.Debug('%%%%%%%%%%%%%parent%%%%%% '+parentchildmap.get(parent).Size());
        if(cmps.id == parent && cmps.Service_Reports__c == parentchildmap.get(parent).Size())
        {
          rixmlSet.add(cmps.RIXML_Code__c);
         String sourceStr = '';
                                Set<String> str=new set<String>();
                                rixmlSet.add(con.Entitled_Document_Groups__c);  
                                System.debug('&*&*&*&*&*&*&*&*'+rixmlset);
                                for(String s:rixmlSet) 
                                {
                                    //  sourceStr += (sourceStr==''?'':',') + s; 
                                    if(s!=null)
                                    {
                                        sourceStr += s;
                                        str.add(s);
                                        sourceStr += ',';
                                    }
                                    system.debug('$%$%$%$%$%$%$%$ sourceStr '+sourceStr );
                                }
                                
                                set<string> memberIds = new set<string>();
                                for (String str1:sourceStr.split(','))
                                {
                                    memberIds.add(str1);
                                    
                                }
                                
                                // System.debug('%^%^%^%^%^%^%^%%%^%^%'+memberIds.remove(null));
                                String uniquemembers = JSON.serialize(memberIds).replaceAll('["\\[\\]]','');               
                                //system.debug(members);
                                system.debug(uniquemembers);
                                
                                con.Entitled_Document_Groups__c=uniquemembers;
                                con.Entitled_Document_Groups_Last_Updated__c=system.today(); 
                                UpdateCon.put(con.id,con);
        }
        }
        }
        }
        }
        
        if(UpdateCon !=null)
        {
            Database.update(UpdateCon.values(),false);
        }
    }
}

Advance thanks for your help.

Thanks.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi,

To update a Contact field based on a Campaign Member record using Process Builder. hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar