+ Start a Discussion
Mario Conteddu 1Mario Conteddu 1 

Checking and Unchecking contact checkbox based on campaign memeber

Hi Guys,
I need to write a trigger which will tick a checkbox in contact when the contact is member of a specific campaign. Furthermore, I need the same checkbox to be un-ticked when the contact has a date (end date) on a custom field which I created in the campaign member object.
I managed to get the checkbox ticked but I can't get it unticked when an end date is added. Can you please help me?
 
trigger updateContact on CampaignMember (before update){
  List<id> contactIds=new List<Id>();
  for(CampaignMember cm:trigger.new){
    if(cm.ContactId != null){
      System.debug('End Date of campaign membership is ' + cm.CM_End_Date__c);
      if(cm.CM_End_Date__c == null && cm.campaignId == '701b0000000FY9Z'){
        contactIds.add(cm.ContactId);
        System.debug('Id is ' + cm.ContactId);
      }  
    }
  }
  List<Contact> contacts=[SELECT Id,    TestTrustee__c FROM Contact WHERE Id IN : contactIds];
  for(Contact c:contacts){
      System.debug('Found contact ' + c);
    c.TestTrustee__c=True;
      system.debug('Trustee status is now '+c.TestTrustee__c );
  }

}

 
Mario Conteddu 1Mario Conteddu 1
Actually, that code I provided above does not work at all now - i must have messed it up.
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Mario,

 

Please update code as below and let me know if you face any issue:

trigger updateContact on CampaignMember (before update){
  List<id> contactIds=new List<Id>();
  List<id> contactIds1=new List<Id>();
  for(CampaignMember cm:trigger.new){
    if(cm.ContactId != null){
      System.debug('End Date of campaign membership is ' + cm.CM_End_Date__c);
      if(cm.CM_End_Date__c == null && cm.campaignId == '701b0000000FY9Z'){
        contactIds.add(cm.ContactId);
        System.debug('Id is ' + cm.ContactId);
      }
		else if(cm.CM_End_Date__c != null)
		{
			contactIds1.add(cm.ContactId);
		}
    }
  }
  List<Contact> contacts=[SELECT Id,TestTrustee__c FROM Contact WHERE Id IN : contactIds];
  if(contacts.size()>0 && contacts!=null)
  {
  for(Contact c:contacts){
      System.debug('Found contact ' + c);
    c.TestTrustee__c=True;
      system.debug('Trustee status is now '+c.TestTrustee__c );
  }
}

List<Contact> contacts1=[SELECT Id,TestTrustee__c FROM Contact WHERE Id IN : contactIds1];
  if(contacts1.size()>0 && contacts1!=null)
  {
  for(Contact c:contacts1){
      System.debug('Found contact ' + c);
    c.TestTrustee__c=False;
      system.debug('Trustee status is now '+c.TestTrustee__c );
  }
}
}
Mario Conteddu 1Mario Conteddu 1
Hi Deepak,

Thanks for correcting my code.

I've tried id but nothing happens. When I add a contact to the campaignid 701b0000000FY9Z, the checkbox does not get ticked in the contact.

The debug shows the following, but it does not actually happen:

12:57:24.0 (129379795)|USER_DEBUG|[23]|DEBUG|Trustee status is now true

 
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Mario,

 

The trigger is on Campaign member. So trigger will run only when campaign member is updated.

This trigger will not fire on any event on Contact

Mario Conteddu 1Mario Conteddu 1
Hi Deepak,

When I add a contact to a campaign, a new campaing member for the same contact gets created. The campaign member creation should fire the trigger, right? I've also tried to updated the campain member, but it still dot not update the checkbox on the contact. 
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Mario,

 

It will only work on updation of Campaign Member.

Mario Conteddu 1Mario Conteddu 1
Unfortunatelly, I've also tried to updated the campain member, but it still dot not update the checkbox on the contact. Maybe I am doing something wrong?
Mario Conteddu 1Mario Conteddu 1
I eventually resolved this using process builder.