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
Todd PlunkTodd Plunk 

Trigger How-To: Track current campaign when lead status changes

Following up from this Question: https://success.salesforce.com/answers?id=9063A000000iRnpQAE

I am trying to track the most current campaign on a lead record when a lead status changes. We built a trigger already that tracks the date a lead status changes (below), but now I want to also track the current (or newest) campaign on the lead record.  Can I do this with a trigger?

Here is the current trigger in place that tracks the date a lead changes status:
// fills in the Recycle_Conversion_Date__c field with today's date
// whenever a lead's status is updated from Recycle to
// A, B, C, or TQL status.
trigger RecycleConversionDate on Lead (before update) {
  for (Lead oldLead : Trigger.old) { 
    Lead newLead = Trigger.newMap.get(oldLead.Id);
    if (oldLead.status == 'Recycle' && 
      (newLead.status == 'A- Lead Pursuing' || 
       newLead.status == 'B- Lead Contacted' || 
       newLead.status == 'C- Lead Responded' ||
        newLead.status == 'TQL - Totally Qualified Lead')) {
         newLead.Recycle_Conversion_Date__c = System.today();
    }
  }
}

Can we change this trigger to insert the most recent campaign on the lead record instead of the date?  I can't seem to find the proper Apex value that would give me a campaign name on a lead record. (aka, I am not a developer)

Use Case... when a lead changes from Recycle to C- Lead Responded, what is the newest, or most current campaign name on that lead record.  I don't really care about the campaign member status, I just want to know the name of the campaign at the time the lead status changes.

Hope this makes sense!  Thanks for the help!
Terence_ChiuTerence_Chiu
Todd, are you trying to identify of all the campaigns where the lead is part of as a campaign member, which campaign is the latest that has been created?
Todd PlunkTodd Plunk
No, only the most recent campaign.  The assumption we are making is that the most recent campaign caused the lead to progress to the next lead stage.

I'm trying to evaluate what campaigns help convert Recycle to C-Leads, and C-Leads to B-Leads, etc...