So I need to create a custom "Add Members" button into Campaigns so Contacts and Leads can be selected from there..basically just a list of Leads+Contacts with checkboxes so users can select what do they want to add to the Campaign and then click on another button called "Add to Campaign". If I create a list of Contacts, it works and they all show up; but once I create a list of Campaign members it gives me this error message:
"Invalid conversion from runtime type List to List"
As always, any help will be very appreciated! This is my code:
public class Pagination_min {
public Integer noofRecords {get; set;}
public integer size {get; set;}
public List<DataPoint> points {get; set;}
public List<Contact> contactsToAdd {get; set;}
Public Apexpages.standardsetController setcon{
get{
if(setCon == null){
size = 20;
String queryString = 'Select Name,Account.Name from Contact order by Name';
setcon = new apexpages.standardsetController(Database.getquerylocator(queryString));
setcon.setpagesize(size);
noofRecords = setcon.getResultsize();
}
return setcon;
}
set;
}
/* This is the list of Contacts that works just fine.
Public list<Contact> getContacts(){
list<Contact> contlist = new list<Contact>();
for(Contact cont : (list<Contact>)setcon.getrecords()){
contlist.add(cont);
}
return contList;
} */
Public list<CampaignMember> getMembers(){
list<CampaignMember> memberlist = new list<CampaignMember>();
for (Contact cont : (list<Contact>)setcon.getrecords()){
CampaignMember contmember = new CampaignMember(
CampaignId='701b0000000T1KW',ContactId = cont.Id);
memberlist.add(contmember);
}
for (Lead ld: (list<Lead>)setcon.getrecords()){
CampaignMember leadmember = new CampaignMember(
CampaignId='701b0000000T1KW',LeadId = ld.Id);
memberlist.add(leadmember);
}
return memberList;
}
Public PageReference Refresh(){
setcon=null;
getMembers();
setcon.setpageNumber(1);
return null;
}
public PageReference AddToCamp () {
contactsToAdd = new List<Contact>();
for(DataPoint point : points) {
if(point.isPrint) {
contactsToAdd.add(point.contactData);
}
}
points.clear();
return null;
}
}
Visualforce Page:
<apex:page Controller="Pagination_min">
<apex:form >
<apex:commandButton action="{!AddtoCamp}" value="Add to Campaign" />
<script type="text/javascript">
function checkAll(cb,filter)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf(filter)!=-1)
inputElem[i].checked = cb.checked;
}
}
</script>
<apex:pageBlock >
<apex:pageBlockSection columns="1" >
<apex:pageBlockTable value="{!Members}" var="c">
<apex:column value="{!c.Contact.Name}"/>
<apex:column value="{!c.Contact.Account.Name}"/>
<apex:column ><apex:facet name="header">Add to Campaign </apex:facet><apex:inputCheckbox id="Selectd_Print" /></apex:column>
</apex:pageBlockTable>
<apex:pageBlockTable value="{!Members}" var="l">
<apex:column value="{!l.Lead.Name}"/>
<apex:column value="{!l.Lead.Company}"/>
<apex:column ><apex:facet name="header">Add to Campaign </apex:facet><apex:inputCheckbox id="Selectd_Print" /></apex:column>
</apex:pageBlockTable>
<apex:panelGrid columns="7">
<apex:commandButton value="|<" action="{!setcon.first}" disabled="{!!setcon.hasPrevious}" title="First page"/>
<apex:commandButton value="<" action="{!setcon.previous}" disabled="{!!setcon.hasPrevious}" title="Previous page"/>
<apex:commandButton value=">" action="{!setcon.next}" disabled="{!!setcon.hasNext}" title="Next page"/>
<apex:commandButton value=">|" action="{!setcon.last}" disabled="{!!setcon.hasNext}" title="Last page"/>
<apex:outputText >{!(setCon.pageNumber * size)+1-size} - {!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>
<apex:commandButton value="Refresh" action="{!refresh}" title="Refresh Page"/>
</apex:panelGrid>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>