Skip to main content
Naveen Ashok Sirivella (Student) a posé une question dans #Apex
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
10 réponses
  1. 2 juil. 2018, 17:31
    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);

    }

     
0/9000