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
mat_tone_84mat_tone_84 

work on campaignmember with trigger

I have to update a custom field of contact if the field status of campaignmember change.

How can I fire on campaignmember with a trigger?

I need a trigger like this:


trigger trigger1 on CampaignMember(after update) { join contact with campaignmember by id.... and then contact.custom_field__c = campaignmember.status }

 thanks

Best Answer chosen by Admin (Salesforce Developers) 
DManelskiDManelski

Currently, triggers are not allowed on second class objects (junction objects) like Campaign Member.  I believe that Salesforce is exploring this functionality in a future release.  Vote up the idea here:

 

Allow Apex triggers on junction tables like CampaignMember

All Answers

DManelskiDManelski

Currently, triggers are not allowed on second class objects (junction objects) like Campaign Member.  I believe that Salesforce is exploring this functionality in a future release.  Vote up the idea here:

 

Allow Apex triggers on junction tables like CampaignMember

This was selected as the best answer
jkucerajkucera

We've heard your votes and Campaign Member triggers are going live with Summer '09.  To do a field update on another object, you can create the custom field and then use code something like the below.  This updates the custom field "Campaign.Last_RSVP_Yes_Time__c" on Campaign when one of it's Campaign Members status changes to RSVP-Yes.

 

trigger UpdateCmpLastRSVPTimeTrigger on CampaignMember (before update) {
Campaign c = [select Id, name from Campaign where Id = :Trigger.new[0].CampaignId limit 1];

for(CampaignMember cm : Trigger.new){
if (cm.Status == 'RSVP-Yes') {
c.Last_RSVP_Yes_Time__c = datetime.now();
}
}


update(c);
}

 

You can change Campaign to Contact and tweak the code to meet your needs.  Make sure you include a check for whether the CM is a contact or lead or you'll get some exceptions thrown.