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
SFTerrSFTerr 

Problem with Apex Class

Hi, 

 

We have a custom object called "Contact Profiles" which writes information back to the campaign member object. So for every campaign member record we have we also have 1 contact profile record.

 

However the problem is that when we update one of our contact profile records this trigger and class should write the same infromation back to the matching campaign member record however instead updates all the campaign members of a related camapign with the same information 

 

Here is the class that writes the information back. 

 

*/
public with sharing class ContactProfileServices {
        
    public static boolean isRunning = false;
    
    public static void updateValuesInCampaignMembers(Map<Id,Contact_Profiles__c> oldContactProfiles, Map<Id,Contact_Profiles__c> newContactProfiles){
        
        final List<CampaignMember> campaingMemberList = new List<CampaignMember>();
        final List<CampaignMember> campaignMemberOnEachCampaign = new List<CampaignMember>();       
        final Set<ID> campaignIDSet = new Set<ID>();
        
        final map<Id,Contact_Profiles__c> campaignIdToContactProfiles = new map<Id,Contact_Profiles__c>();      
        
        //When a new Contact Profile is created old Contact Profile would be null.
        if(oldContactProfiles == null)
            return;
        
        final List<Contact_Profiles__c> changedContactProfiles = checkForChangedFields(oldContactProfiles, newContactProfiles);
        if(changedContactProfiles.isEmpty())
            return;
        
        for(Contact_Profiles__c currentContact : changedContactProfiles){           
            
            campaignIDSet.add(currentContact.campaign__c);
            campaignIdToContactProfiles.put(currentContact.Campaign__c,currentContact);     
        }
        
        campaignMemberOnEachCampaign.addAll([Select Id,
                                        campaignId,
                                        Profile__c,
                                        Source2__c,
                                        Status 
                                        from CampaignMember 
                                        where campaignId IN :CampaignIDSet]);
                                        
        
            
            for(CampaignMember currentCampMem : CampaignMemberOnEachCampaign){
                
                if(campaignIdToContactProfiles.containsKey(currentCampMem.campaignId)){
                
                    currentCampMem.Profile__c = campaignIdToContactProfiles.get(currentCampMem.campaignId).Profile__c;
                    currentCampMem.Source2__c =campaignIdToContactProfiles.get(currentCampMem.campaignId).Source2__c;
                    currentCampMem.status  = campaignIdToContactProfiles.get(currentCampMem.campaignId).Campaign_Member_Status__c;
                                        
                    campaingMemberList.add(currentCampMem);
                    
                    system.debug('***'+campaingMemberList);
                
                }
            }   
            
            
        isRunning =  true;                                                                      
        updateCampaigmMembers(campaingMemberList);
        isRunning = false;
    }
    
    private static void updateCampaigmMembers(final List<CampaignMember> campaingMemberList){
        update campaingMemberList;      
    }
    
    private static list<Contact_Profiles__c> checkForChangedFields(final Map<Id,Contact_Profiles__c> oldContactProfiles, final Map<Id,Contact_Profiles__c> newContactProfiles){
        
        final list<Contact_Profiles__c> changedContactProfiles = new list<Contact_Profiles__c>();
        for(Contact_Profiles__c newContactProfile :newContactProfiles.values()) {
            Contact_Profiles__c oldContactProfile = oldContactProfiles.get(newContactProfile.Id);
            if(oldContactProfile.Profile__c != newContactProfile.Profile__c || 
               oldContactProfile.Source2__c != newContactProfile.Source2__c ||
               oldContactProfile.Campaign_Member_Status__c != newContactProfile.Campaign_Member_Status__c){
                changedContactProfiles.add(newContactProfile);
            }
        }
        
        return changedContactProfiles;
    }
    
    //kkrupal: moved this from opportunityservices. this belongs here
    public static  Map<Id, Contact_Profiles__c> getContactOnContactProfile(final set<Id> contactProfileIds){
        return new Map<Id,Contact_Profiles__c>([SELECT Id, Contact__c FROM Contact_Profiles__c WHERE Id IN :contactProfileIds]);
    }   
    
}

 The fields we are updating that need to write back are Profile, Source, List and Campaign Member Status. 

 

The person who created this code no longer works for us and I am not an advanced coder but any help you could provide will be much apprechiated. 

 

Thanks,

Michael