1

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>

1 Answer 1

1

Note the query string you have in this method below for Contacts:

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));

You call the above from 4 places, including once in this method for contacts:

Public list<Contact> getContacts(){
    list<Contact> contlist = new list<Contact>();
     for(Contact cont : (list<Contact>)setcon.getrecords()){

         contlist.add(cont);

But you have nothing equivalent to the 1st method with the query string which handles Leads. You call the setcon query method again to get contacts and also to get your leads when you call them from the method below:

Public list<CampaignMember> getMembers(){
    list<CampaignMember> memberlist = new list<CampaignMember>();

That would appear to be the source of your issue with Leads.

It would seem you either need additional methods just for leads that provide the same functionality or else you need to modify your methods to pass object identifiers and query strings so you can run dynamic queries for Leads or Contacts. Personally, I'd recommend doing the latter.

But that's the cause of your error. You're trying to query through Contacts for Leads and construct a list of them using methods intended for Contacts.

2
  • That is true! I added: <br> Public Apexpages.standardsetController setld{ get{ if(setld == null){ size = 20; String queryString = 'Select Name,Company from Lead order by Name'; setld = new apexpages.standardsetController(Database.getquerylocator(queryString)); setld.setpagesize(size); nooflead = setld.getResultsize(); } return setld; } set; }
    – Leo
    Commented May 26, 2015 at 18:08
  • Please help the rest of the community by marking this answer as solving your question if it resolved your issue.
    – crmprogdev
    Commented May 26, 2015 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .