+ Start a Discussion
Naveen Ashok SirivellaNaveen Ashok Sirivella 

Populate campaign details on lead record

Hi All,

I want to update campaign name,primary product on Lead record .

Please find the below code

Public static void After_UpdateLeadCampaignDetails()
      {
          List<CampaignMember> camlist = (List<CampaignMember>)trigger.new;
          System.debug('Values in the camlist:========================>'+camlist);
          Set<Id> leadid = new Set<Id>();
          
          List<Lead> leadlist = new List<Lead>();
          for(CampaignMember cm:camlist)
          {
              Lead lea = new Lead();
                if(lea.id == cm.LeadId)
                {
                    lea.Campaign_Name__c = cm.Campaign.name;
                    leadlist.add(lea);
                }
          }
          
          database.update(leadlist);
          
      }

Please help any one
PawanKumarPawanKumar
https://salesforce.stackexchange.com/questions/88887/trigger-to-tie-a-new-lead-back-to-campaign

Regards,
Pawan Kumar
Abdul KhatriAbdul Khatri
I believe you are calling this method from the trigger.

The issue with your code is you are referring trigger.new which is only available with trigger context. You also have some unnecessary code. Here is the simple code for your requirements
 
Public static void After_UpdateLeadCampaignDetails(List<Id> idList)
{
    List<CampaignMember> camlist = [Select Campaign.Name, LeadId From CampaignMember Where Id IN :idList];
    System.debug('Values in the camlist:========================>'+camlist);
    
    if(camlist == null) return;
    
    List<Lead> leadToUpdateList = new List<Lead>();
   
    for(CampaignMember cm:camlist)
    {
        Lead lead = new Lead (Id = cm.LeadId);
        lead.Camapign_Name__c = cm.Campaign.Name;

        leadToUpdateList.add(lead);
    }
    
    if(!leadToUpdateList.isEmpty())
        database.update(leadlist);
    
}
I guess you may have triigger that somewhat looks like this
 Please change the ,className to something you have.
trigger UpdateLeadCampaign on CampaignMember (after update) {

         <className>.After_UpdateLeadCampaignDetails(trigger.new);

}

 
Naveen Ashok SirivellaNaveen Ashok Sirivella
Sorry above code is not solved my requirements.

trigger:
trigger CampaignMemberTrigger on CampaignMember (before insert,before update,before delete,after insert,
                                   after update,after delete,after undelete) 
{
    
        TriggerRouter.Handler(new CamapignMemberHandeler());    
}

Class
public class CampaignMemberHelper 
{
     
     Public static void After_UpdateLeadCampaignDetails()
      {
          List<CampaignMember> camlist = (List<CampaignMember>)trigger.new;
          System.debug('Values in the camlist:========================>'+camlist);
          Set<Id> leadid = new Set<Id>();
          
          List<Lead> leadlist = new List<Lead>();
          for(CampaignMember cm:camlist)
          {
              Lead le = new Lead (Id = cm.LeadId);
              le.Campaign_Name__c = cm.Campaign.Name;
              leadlist.add(le);

          }
          
          if(!leadlist.isEmpty())
            {
                System.debug('size of the leadlist:=============================================>'+leadlist.size());
                database.update(leadlist);
             }
          
      }

}
Abdul KhatriAbdul Khatri
What is the issue with your code?
Naveen Ashok SirivellaNaveen Ashok Sirivella
Actually i am using trigger interface in above trigger 
 and modified your code according to your comments but the campaign name is not updating on lead record
Abdul KhatriAbdul Khatri
That's fine, my code may not work as the way you are using the handler. My Question is your code is also not working.

Can you please share the class CamapignMemberHandeler
Naveen Ashok SirivellaNaveen Ashok Sirivella
trigger
---------------------
trigger CampaignMemberTrigger on CampaignMember (before insert,before update,before delete,after insert,
                                   after update,after delete,after undelete) 
{
    
        TriggerRouter.Handler(new CamapignMemberHandeler());    
}

campaign member
-------------------------------
public class CamapignMemberHandeler implements TriggerInterface
{
     public void isActive()
     {
         
     }
     public void beforeInsert()
     {
         
     }
     public void beforeUpdate()
     {
         
     }
     public void beforeDelete()
     {
         
     }
     public void afterInsert()
     {
           
      
     }
     public void afterUpdate()
     {
          CampaignMemberHelper.After_UpdateLeadCampaignDetails(); 
     }
     public void afterDelete()
     {
         
     }
     public void afterUndelete()
     {
         
     }
}

helper
----------------------
public class CampaignMemberHelper 
{
     
     Public static void After_UpdateLeadCampaignDetails()
      {
          List<CampaignMember> camlist = (List<CampaignMember>)trigger.new;
          System.debug('Values in the camlist:========================>'+camlist);
          Set<Id> leadid = new Set<Id>();
          
          List<Lead> leadlist = new List<Lead>();
          for(CampaignMember cm:camlist)
          {
              Lead le = new Lead (Id = cm.LeadId);
              le.Campaign_Name__c = cm.Campaign.Name;
              leadlist.add(le);

          }
          
          if(!leadlist.isEmpty())
            {
                System.debug('size of the leadlist:=============================================>'+leadlist.size());
                database.update(leadlist);
             }
          
      }

}
Naveen Ashok SirivellaNaveen Ashok Sirivella
can i have your phone no
Abdul KhatriAbdul Khatri
where you from?

You haven't said what is the problem with your code. Please share the Trigger Interface code
 
Naveen Ashok SirivellaNaveen Ashok Sirivella
I am from andhra
trigger interface doesn't have any problem