Activity Forums Salesforce® Discussions How to associate multiple leads to campaign in Salesforce?

  • shariq

    Member
    September 14, 2017 at 2:07 pm

    Hi Shaharyar,

    apex controller : -

    public class CampaignLeads
    {
    public List<AddLeads> addFewLeads{get; set;}
    public List<CampaignMember> campMems ;
    public List<Id> ids;
    public Campaign camp;
    public CampaignLeads(ApexPages.StandardController sc)
    {
    ids = new List<Id>();
    camp = new Campaign();
    addFewLeads = new List<AddLeads>();

    for(Lead lead: [SELECT Id, LastName FROM Lead WHERE Id NOT IN (SELECT LeadId FROM CampaignMember WHERE CampaignId =: ApexPages.currentPage().getParameters().get('id')) LIMIT 50 ])
    {
    AddLeads objAddLeads = new AddLeads(lead);
    addFewLeads.add(objAddLeads);
    }
    campMems = new List<CampaignMember>();
    }

    public void insertLead()
    {
    camp = [SELECT Id FROM Campaign WHERE Id =: ApexPages.currentPage().getParameters().get('id')];
    for(AddLeads addLead : addFewLeads)
    {
    if(addLead.selected == true)
    {
    CampaignMember campMem = new CampaignMember();
    campMem.CampaignId = camp.Id;
    campMem.LeadId = addLead.lead1.Id;
    campMems.add(campMem);
    }
    }
    insert campMems;
    }

    public class AddLeads
    {
    public Lead lead1{get; set;}
    public Boolean selected{get; set;}
    public AddLeads(Lead wrpLead)
    {
    lead1 = wrpLead;
    selected = false;
    }
    }
    }

    Visualforce Page :-

    <apex:page standardController="Campaign" extensions="CampaignLeads" >
    <apex:form id="formID">
    <apex:pageBlock >
    <apex:pageBlockButtons >
    <apex:commandButton value="Insert Lead" reRender="formID" action="{!insertLead}" oncomplete="CampaignPage()" />
    </apex:pageBlockButtons>
    <apex:pageBlockTable value="{!addFewLeads}" var="le" columns="4">
    <apex:column headerValue="Select">
    <apex:inputCheckbox value="{!le.selected}"/>
    </apex:column>
    <apex:column value="{!le.lead1.LastName}">
    </apex:column>
    </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
    <script>
    function CampaignPage()
    {
    window.top.location='/{!Campaign.Id}';
    }
    </script>
    </apex:page>

    Put this VF page on Campaign detail page layout.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos