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
Divyansh Mishra 6Divyansh Mishra 6 

take the user to a visualforce page, where he can quickly type few lead names, and hit a button to attach them with a Campaign.

Hey Folks, I need to create an accessory visualforce page for Campaign, which lets a user quickly associate multiple leads to a campaign.
This is my Apex class and VFP: 
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>

but this is not working. Can someone help in figuring out the possible error
Divyansh Mishra 6Divyansh Mishra 6
This is the error :

List has no rows for assignment to SObject
Error is in expression '{!insertLead}' in component <apex:commandButton> in page campaignleadvfp: Class.CampaignLeads.insertLead: line 23, column 1

An unexpected error has occurred. Your development organization has been notified.
Maharajan CMaharajan C
Hi Divyansh,

1.  Put one Campaign Record Id in  end of your current page URL. like below:
https://rajaccountgeolocationapp-dev-ed--c.ap6.visual.force.com/apex/CampaignLeadsVF?id=701280000001uiA

2. Or place your VF page in Campaign record page.

Thanks,
Maharajan.C