+ Start a Discussion
jpb@uhcjpb@uhc 

CampaignMember Trigger Help

We currently have a trigger (see below) that creates a record in a custom object called LocationCampaignAssociations for every Campaign Member record created. Essentially the custom object is a junction object to create a relationship between the Campaign and the Account for the Contact that is being added to the Campaign.

 

A new request is that we remove the record from the custom object, in other words the Account to Campaign association, if the there are NO longer any CampaignMembers (Contacts) tied to that Location.

 

Examples:

 

3 Contacts all at the same Account, 1 Contact removed from the Campaign, do not remove the record from the custom object...keeping the Account to Campaign association.

 

3 Contacts all at the same Account, all 3 Contacts removed from the Campaign, remove the record from the custom object since the Account should no longer be associated with the campaign either.

 

Any ideas on how to modify our trigger to do so?

 

trigger CreateLocCampMembers on CampaignMember (after insert) {

List<LocationCampaignAssociations__c> createLoc = new List <LocationCampaignAssociations__c> {};

for (CampaignMember cm : trigger.new) {
createLoc.add(new LocationCampaignAssociations__c (Campaign__c = cm.CampaignId, Location__c = cm.Location_ID__c));
}
try {
insert createLoc;
}
catch (Exception Ex)
{
system.debug(Ex);
}
}
apex_keenapex_keen

I'm little confused regarding your setup..particularly junction object.. Not sure, how account is coming into picture here, instead of location..But from whatever i've get...can suggest you something like this :

 

trigger CreateDelLocCampMembers on CampaignMember (after insert, before delete) {
If(trigger.isInsert)
{

...your existing logic
}

If(trigger.isdelete)
{
set<id> CampMemIds = trigger.oldmap.keyset();

aggregateResult[] total = [select count(id) cnt, Location_id__c from campaignMember where id in : CampMemIds group by Location_id__c ];
for(aggregateResult ar :total)
{
if (integer.valueOf(ar.get('cnt'))==1)
{
LocationCampaignAssociations__c loc = [select id from LocationCampaignAssociations__c where location__c = : String.valueOf(ar.get('Location_id__c')) ];
delete loc;
}
}
}
}

 

Try and get back to me , in case of further questions.

 

 

 

 

jpb@uhcjpb@uhc

Thanks for the response! I guess I should have explained that we renamed Accounts to Locations. I used that suggested code you gave me and get the following error when removing a CampaignMember:

 

CreateDelLocCampMembers: execution of BeforeDelete

caused by: System.UnexpectedException: field 'Location_id__c' can not be grouped in a query call

Trigger.CreateDelLocCampMembers: line 24, column 27

 

Here is the new code that I am using:

trigger CreateDelLocCampMembers on CampaignMember (after insert, before delete) {

If(trigger.isInsert)
{
List<LocationCampaignAssociations__c> createLoc = new List <LocationCampaignAssociations__c> {};

for (CampaignMember cm : trigger.new) {
createLoc.add(new LocationCampaignAssociations__c (Campaign__c = cm.CampaignId, Location__c = cm.Location_ID__c));
}

try {
insert createLoc;
}
catch (Exception Ex)
{
system.debug(Ex);
}
}

If(trigger.isdelete)
{
set<id> CampMemIds = trigger.oldmap.keyset();

aggregateResult[] total = [select count(id) cnt, Location_id__c from campaignMember where id in : CampMemIds group by Location_id__c ];
for(aggregateResult ar :total)
{
if (integer.valueOf(ar.get('cnt'))==1)
{
LocationCampaignAssociations__c loc = [select id from LocationCampaignAssociations__c where location__c = : String.valueOf(ar.get('Location_id__c')) ];
delete loc;
}
}
}
}