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
MktdevMktdev 

Trigger to update contact level checkbox custom field based on campaign member status.

Hi,

 

I am new to salesforce trigger.NEED HELP!!!

 

I am trying to write a trigger to update contacts field with the response received tracked in campaign member object.

 

I have one field call Subcribered at contact level.When a contact is tagged for campaign then we are waiting for his reply after receiving the reply we update campaign member status.

 

So is there any way we can update the Subcribered checkbox as check once the campaign member status changed.

 

Suppose:

ABC has subscibed for the email then we change it status to Responed then salesforce should automatically update the his Subscribed checkbox.

 

Please help

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
frederic baudaxfrederic baudax

Morning,

That line simply mention on which records the trigger is executed.
To know what is returned in your triggers, use System.debug statements.

 

There was indeed a slight error there =>   contactIds.add(cm.Id);

You can use the code bellow which i used in sandbox to test it and everything works well. I've left the system.debug statements on purpose so you understand how they work. But they should be removed afterwards.

 

 

trigger updateContact on CampaignMember (before update){
  List<id> contactIds=new List<Id>();
  for(CampaignMember cm:trigger.new){
    if(cm.ContactId != null){
      System.debug('Status is ' + cm.Status);
      if(cm.Status == 'Subscribed'){
        contactIds.add(cm.ContactId);
        System.debug('Id is ' + cm.ContactId);
      }  
    }
  }
  List<Contact> contacts=[SELECT Id, Subscribed__c FROM Contact WHERE Id IN : contactIds];
  for(Contact c:contacts){
      System.debug('Found contact ' + c);
    c.Subscribed__c=True;
  }
  try{
    update contacts;
  System.debug('Contacts to update list size ' + contacts.size());
  }catch (DMLException e){
    system.debug('contact update failed: '+e);
  }
}

 

 

Kr,

Fred

 

All Answers

ScoobieScoobie

This doesn't sound like you need a trigger. You could easily do this with a workflow from what you have described.

MktdevMktdev

Thanks Scoobie!!!

 

Work flow for one object only where as this requires two object one is campaign member which is a object with campaign and another one is contact.

 

So first of all I have to check campaign member status on after insert and update where status is equals to responed and then update the field by referencing the contacts.

 

Hope this help to clear my view.

 

Thanks in advance!!!

jkucerajkucera

 

trigger updateContact on CampaignMember (before update){
  List<id> contactIds=new List<Id>();
  for(CampaignMember cm:trigger.new){
    if(cm.ContactId!=null){
      if(cm.Status='Subscribed'){
        contactIds.add(cm.Id);
      }  
    }
  }
  List<Contact> contacts=[SELECT Id, Subscribed__c FROM Contact WHERE Id IN : contactIds];
  for(Contact c:contacts){
    c.Subscribed__c=True;
  }
  try{
    update contacts;
  }catch (DMLException e){
    system.debug('contact update failed: '+e);
  }
}

 

 

 

MktdevMktdev

Hi John,

 

Thanks a lot!

 

This code is not working.It is showing error :

 

"Error: Compile Error: Condition expression must be of type Boolean at line 5 column 7"

 

And according to me it is considering Campaign-Status which is checkbox(True or False)  field.How to refer to "Campaign Member Status" field.

 

Thanks in advance!!!

 

 

Regards,

MktDev   

MktdevMktdev

Hi John,

 

I have updated the code "if(cm.Status='Subscribed'){" with "if(cm.Status!='Subscribed'){" and Code is saved and trigger created,

 

But When I update the member status trigger is not responding.Nothing is been done.

 

Regards,

 

frederic baudaxfrederic baudax

Hi,


Use this instead

 

 

if(cm.Status =='Subscribed'){

 

"!=" means not equal to and "==" equals to when in your if condition. The way you changed it, everytime a campaign member record is changed with a status different from  'Subscribed' it will trigger the update.

 

Kr

MktdevMktdev

Hi Frederic,

 

Thanks a lot!

 

I have changed it and checked the log and found that the ID which is storing in the List is on Campaign Member ID which is not the contactID I tried to link Contact field of Members to the list but it not allowing me to save.How to find the contact ID for member ID they have chid relationship.

 

This is the output of the code:

 

5:10:29.54|CODE_UNIT_STARTED|[EXTERNAL]updateContact on CampaignMember trigger event BeforeUpdate for 00v90000000q1dN, 00v90000000q1dP, 00v90000000q1dQ

 

Bold ids are not contact ID its member ID.

 

Please help.

 

Thanks in advance

frederic baudaxfrederic baudax

Morning,

That line simply mention on which records the trigger is executed.
To know what is returned in your triggers, use System.debug statements.

 

There was indeed a slight error there =>   contactIds.add(cm.Id);

You can use the code bellow which i used in sandbox to test it and everything works well. I've left the system.debug statements on purpose so you understand how they work. But they should be removed afterwards.

 

 

trigger updateContact on CampaignMember (before update){
  List<id> contactIds=new List<Id>();
  for(CampaignMember cm:trigger.new){
    if(cm.ContactId != null){
      System.debug('Status is ' + cm.Status);
      if(cm.Status == 'Subscribed'){
        contactIds.add(cm.ContactId);
        System.debug('Id is ' + cm.ContactId);
      }  
    }
  }
  List<Contact> contacts=[SELECT Id, Subscribed__c FROM Contact WHERE Id IN : contactIds];
  for(Contact c:contacts){
      System.debug('Found contact ' + c);
    c.Subscribed__c=True;
  }
  try{
    update contacts;
  System.debug('Contacts to update list size ' + contacts.size());
  }catch (DMLException e){
    system.debug('contact update failed: '+e);
  }
}

 

 

Kr,

Fred

 

This was selected as the best answer
MktdevMktdev

Hi Frederic,

 

This is great!!! Much appreciated!

 

Regards,

Mktg

jkucerajkucera

Oops :)  Thanks for the fix.