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
lreedlreed 

update Campaign Members Custom Field when an Event is added to the campaign

I am looking for the best way to accomplish updating Campaign Members Custom Field when an Event is added to the Campaign.
I don;t think its possible with process flow.  Any Trigger examples would be greatly appreciated.
James LoghryJames Loghry
The Process Builder implementation is rather limited.  You can fire a process when it's related to a specific campaign (filtering on where WhatId = the campaign id).  You can also update the "EventRelation" related records from the Event, but it doesn't sound like that will work for you.

You could take the Process a step further, and have it call a Flow:
  1. The Flow would take an input variable for the Campaign Id (WhatId of th event).
  2. The Flow would do a Fast Lookup of Campaign Members, where the CampaignId = the input variable in Step 1.
  3. Create a Loop element and Assignment elements that loop through the Campaign Member sobject collection, and set the custom fields
  4. Create a Fast Update element to update the campaign members collection variable.
Otherwise you're looking at a trigger that does same logic.
Nisar799Nisar799
Hi Lawrence,
Here you go with the trigger code.
trigger updateCampaign on Event (after update,after insert) {
    Set<Id> campaignId = new Set<Id>(); 
    
    for(Event e : trigger.new){
            String whatId = e.WhatId+'';
            if(whatId.startsWith('701') && (e.Subject== 'Call' && Trigger.isInsert) ||
               whatId.startsWith('701') && (e.Subject== 'Call' && Trigger.isUpdate && Trigger.oldMap.get(e.id).Subject!= 'Call') ){
                      // COLLECTING ALL campaign IDs on which event is created.
                      campaignId.add(e.WhatId);
            }
    }
    // IN THE campaignId you have campaign Ids 
    if(campaignId.size()>0){
        // FETCHING ALL Campaign members.
        List<campaignMember> cMemberToUpdate = [SELECT id FROM campaignMember WHERE campaignId IN :campaignId];
        for(campaignMember cMember : cMemberToUpdate){
            cMember.Custom_Phone__c = '18018018020';
            // UPDATE YOUR CUSTOM FIELD HERE.
        }
        update cMemberToUpdate;
    }
}

Please Modify this sample code as per your requirement. I hope this will help
Thanks,
799 Coder
lreedlreed
Nisar  below is my modified code. It executes without issue, except that my custom field does not get updated.
I placed debug statements in the code and it appears that the line of code to update the field does  Not  get executed, but looking at the record
in the UI  shows it has not been updated.

 
trigger UpdateCampaignMember on Event (before insert) {
   Set<Id> campaignId = new Set<Id>(); 
   String Recordtype; 
// create a map  between record type name and id for easy retrieval
   Map<String, String> EventRecordTypes;
   List<RecordType> rtypes = [Select Name, Id From RecordType 
                              where sObjectType='Event' and isActive=true];
   EventRecordTypes = new Map<String,String>{};                 
   for(RecordType rt: rtypes) {
           		EventRecordTypes.put(rt.Name,rt.Id);
        	}
    
 //////////////////////////////////////////////////   
    for(Event e : trigger.new){
            String whatId = e.WhatId+'';
        	Recordtype = e.RecordTypeId;
            
           if(whatId.startsWith('701') &&  Trigger.isInsert){
                      // COLLECTING ALL campaign IDs on which event is created.
                      campaignId.add(e.WhatId);
            }
    }
    // IN THE campaignId you have campaign Ids
       if(campaignId.size()>0){
        // FETCHING ALL Campaign members.
        List<campaignMember> cMemberToUpdate = [SELECT id FROM campaignMember WHERE campaignId IN :campaignId];
                        for(campaignMember cMember : cMemberToUpdate){
            If (Recordtype == EventRecordTypes.get('ID INS Event')) {
            	 cMember.InsCheckIn__c = True;    
            }
           
            // UPDATE YOUR CUSTOM FIELD HERE.
        }
                update cMemberToUpdate;
    }

}


 
lreedlreed
Line 34 DOES seem to get executed when I Add Debug statements
 
lreedlreed
Should this work on Bulk Event Inserts?
Nisar799Nisar799

Hi Ireed,
Yup the code will work for bulk data, there is a little bit modification with the recordType is needed, See the updated code below
 

trigger UpdateCampaignMember on Event (before insert) {
	Set<Id> campaignId = new Set<Id>(); 
	String Recordtype; 
	
	//create a map  between record type name and id for easy retrieval
	Map<String, String> EventRecordTypes;
	List<RecordType> rtypes = [Select Name, Id From RecordType 
                              where sObjectType='Event' and isActive=true];
	EventRecordTypes = new Map<String,String>{};                 
	for(RecordType rt: rtypes){
    	EventRecordTypes.put(rt.Name,rt.Id);
    }
    
	//////////////////////////////////////////////////   
    for(Event e : trigger.new){
        String whatId = e.WhatId+'';
        Recordtype = e.RecordTypeId;
        if(whatId.startsWith('701') &&  Trigger.isInsert && Recordtype == EventRecordTypes.get('ID INS Event')){
            // COLLECTING ALL campaign IDs on which event is created.
			// These campaigns are filtered with RecordType
            campaignId.add(e.WhatId);
        }
    }
    //IN THE campaignId you have campaign Ids
    if(campaignId.size()>0){
        // FETCHING ALL Campaign members.
        List<campaignMember> cMemberToUpdate = [SELECT id FROM campaignMember WHERE campaignId IN :campaignId];
        for(campaignMember cMember : cMemberToUpdate){
            cMember.InsCheckIn__c = True;    
            // UPDATE YOUR CUSTOM FIELD HERE.
        }
        update cMemberToUpdate;
    }
}

Please have a look on this , I hope this will help.
lreedlreed
Thanks Nisar  I want this to work for all recordtypes, but I need it to update when the Contact.ID of the Member Matches the WhatID on the Event.
I changed the cmemberUpdate list to the following but it seems to compare each campaign member to  one event although mutiple events have been added..

List<campaignMember> cMemberToUpdate = [SELECT id FROM campaignMember WHERE status = 'Attended' and contact.id = :WhatID
and campaignId IN :campaignId];