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
chrissy111chrissy111 

Simple Apex Trigger: Update Lookup field on CampaignMember Object

On the standard CampaignMember object, I have a custom lookup field that I am trying to populate....It links to the Lead object and is called "SFDC_Lead__c".

 

I'm trying to create a trigger that populates this fields with the value from the standard campaignmember field "Lead".  This is what I have so far, although it doesn't work:

 

trigger SetCampaignMemberLeadField on CampaignMember (before insert, before update) {    for (CampaignMember a : Trigger.new) {        a.SFDC_Lead__c = a.Lead.Id;    }}

 

Best Answer chosen by Admin (Salesforce Developers) 
hisrinuhisrinu
Try the below one. trigger SetCampaignMemberLeadField on CampaignMember (before insert, before update) { for (CampaignMember a : Trigger.new) { a.SFDC_Lead__c = a.LeadId; }}

All Answers

hisrinuhisrinu
Try the below one. trigger SetCampaignMemberLeadField on CampaignMember (before insert, before update) { for (CampaignMember a : Trigger.new) { a.SFDC_Lead__c = a.LeadId; }}
This was selected as the best answer
chrissy111chrissy111
Perfect!  Thanks so much!!
jkucerajkucera

btw-you'll want to remove converted leads or you'll get an error every time you try to edit a campaign member that points to the contact from a converted lead.  ContactID is always present for converted leads so this is the simplest check.

 

 

trigger SetCampaignMemberLeadField on CampaignMember (before insert, before update) { for (CampaignMember a : Trigger.new) { if(a.ContactID==null){ a.SFDC_Lead__c = a.LeadId; } } }

 

 

 

chrissy111chrissy111

Thanks, John!  Great point.  So, on the campaign member, I also have a custom lookup field to the Contact object.  I modified your suggestion to be an "if else" so that the Contact field will update, too:

 

trigger SetCampaignMemberLeadContactField on CampaignMember (before insert, before update) { 
  for (CampaignMember a : Trigger.new) { 
    if(a.ContactID==null){
      a.SFDC_Lead__c = a.LeadId; }
  else
  if(a.ContactID!=null){
     a.SFDC_Contact__c = a.ContactId;
     a.SFDC_Lead__c = null;
    }
  }
 }

 

This works to update both fields, and it clears out the SFDC_Lead__c field after the lead is converted.  The only limitation is that the CampaignMember object has to be updated after a lead is converted in order for the custom SFDC_Contact__c field to fill in.  Still pretty cool, though, because now we can pull any Lead or Contact field onto a Campaign Member report or list view.

 
jkucerajkucera

Makes sense - if you want this to fire upon lead convert, you could have 2 triggers:

1) CM Insert trigger to populate leadID or Contact id

2) Contact insert/update trigger to remove LeadID & add ContactID when a lead is converted to either a new contact or merged into an existing one

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

Hi,
I have question about trigger.I create a custom object ="Product" on opportunity object .I create three (3) fields on custom object = Product and the filed description is below:
Field # 1:Application Received By(which is lookup(user)field)
field #2 : Closing Stage (pick list field; option is recieved)
field # 3: Closing Stage Owner ((which is lookup(user)field)

I have the requirment that :
If Application Received By not null
then
condition # 1 : set 
Closing Stage = recieved
and 

condition # 2 : set Closing Stage Owner = Application Received By

Example: If  
Application Received By = steve then according to workflow it should update the Closing Stage = recieved and Closing Stage Owner =steve

I create workflow for this and i am able to satisfy the condition # 1  but I am not to create work field update for condition # 2. 
I am new to salesforce and could anyone of you help me for writing this trigger.

thanks

jkucerajkucera

Its a bit off topic for this thread, but why did you create a custom object for project?  I haven't looked into it in a while, but there might be "cross object workflow" to do what you are depicting below if you use the standard products object.  

 

If your custom object is has a lookup to oppty, to build a trigger you'd need to query the oppty to get the data like Closing Stage & Owner to update your fields.