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
gokubigokubi 

Fire trigger on Change to Campaign Membership

I posted a short movie of Campaign Membership calculations I'm running and rolling up to the Contact record. The movie shows how I modeled this to support community organizing by environmental groups:

My post

I would love this to be Apex, but I can't trigger on CampaignMember, so I wrote an S-Control that the user has to click when they want to recalc camapaign memberships for all Contacts. In the comments of my post, Tom Tobin had the idea of firing a trigger on a change to the Campaign's NumberOfResponses field.

However, it appears that changes to this rollup field are not considered database updates--my trigger isn't firing. Is that correct? Is there any way to fire a trigger based on a change to Campaign Membership? Any novel ideas out there?

Thanks,
Steve
Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

Campaign Member triggers will be released in Summer '09 which make this a lot easier To do calculations based on a change to Campaign Member, you can use the following code as an example.  First you'd need to create a custom field on the object to sum up the results.  In my example, this is a Campaign custom field:

 

trigger UpdateCmpRegistered on CampaignMember (before update) {

    Campaign c = [select Id,Registered__c, name from Campaign where Id = :Trigger.new[0].CampaignId limit 1];

   

    for(CampaignMember cm : Trigger.new){

        if (cm.Status == 'RSVP-Yes') {

          if (c.Registered__c== null) {

              c.Registered__c=0;

              }

          c.Registered__c += 1;

        }

     }

   

    update(c);

}

 

 

Note that your code should have tests & better error handling and that this is specifically simple for proof of concept.

All Answers

jkucerajkucera

Campaign Member triggers will be released in Summer '09 which make this a lot easier To do calculations based on a change to Campaign Member, you can use the following code as an example.  First you'd need to create a custom field on the object to sum up the results.  In my example, this is a Campaign custom field:

 

trigger UpdateCmpRegistered on CampaignMember (before update) {

    Campaign c = [select Id,Registered__c, name from Campaign where Id = :Trigger.new[0].CampaignId limit 1];

   

    for(CampaignMember cm : Trigger.new){

        if (cm.Status == 'RSVP-Yes') {

          if (c.Registered__c== null) {

              c.Registered__c=0;

              }

          c.Registered__c += 1;

        }

     }

   

    update(c);

}

 

 

Note that your code should have tests & better error handling and that this is specifically simple for proof of concept.

This was selected as the best answer
sandersensandersen

Really exciting John, nice work!

 

Steve