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
RajashriRajashri 

Compile Error: Initial term of field expression must be a concrete SObject: LIST<Lead>

Hi

For below code i am getting the error  Initial term of field expression must be a concrete SObject: LIST<Lead>

Can anyone please help?

Below is my code
public with sharing class singleListView {
    public Campaign camp {get; set; }
    public List<Lead> lead {get;set;}
    
    public singleListView(ApexPages.StandardController controller) {
        camp = (Campaign)controller.getRecord();
        
    }

   
   private List<Schema.Lead> CampaignMembers;
    public List<Schema.Lead> getCampaignMembers() {
      CampaignMembers=[Select Id,Name,(Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,LeadID,Lead.Phone,Lead.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone  From CampaignMembers where CampaignId =:camp.Id and Status != '' and LeadId != null), 
(Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
From Lead  where Name !=NULL and Id= :lead.Id order by LastModifiedDate desc];

       return CampaignMembers;
       
     }
         }     
         
pconpcon
your CampaignMembers should be List<Lead> instead of List<Schema.Lead>  or you can assign it to this.lead instead.
RajashriRajashri
Thanks pcon.I have changed itto List<Lead> .There are many leads How can i iterate through the leads and get the lead associated with the Current campaign member..

I am trying to display campaign members and associated lead history..
Can you please help?
Below is my controller code..


public with sharing class CampaignMemController {
    public Campaign camp {get; set; }
    public CampaignMemController(ApexPages.StandardController controller) {
     camp = (Campaign)controller.getRecord();
    }
   public List<Lead> lead= [SELECT Id,Name FROM Lead];
   public List<Lead> leadlist = new list<Lead>();
    public List<CampaignMember> cmlist= new List<CampaignMember>();

    public List<Lead> CampaignMembers;
    public List<Lead> getCampaignMembers() {
         CampaignMembers=[Select Id,Name,(Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,LeadID,ContactID,Lead.Phone,Lead.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone  From CampaignMembers where CampaignId =:camp.Id and Status != ''),
     (Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1)  from Lead where ID=:lead[0].id];
    return CampaignMembers;
    
   
}

}
 
pconpcon
This is how I would do it
 
public with sharing class CampaignMemController {
    public Campaign camp {
        get;
        set;
    }

    public List<Lead> leads {
        get;
        set;
    }   
        
    public List<CampaignMember> campaignMembers {
        get;
        set;
    }           
                
    public CampaignMemController(ApexPages.StandardController controller) {
        this.camp = (Campaign) controller.getRecord();
        this.campaignMembers = [
            select
                CampaignId,
                Campaign.Name,
                Campign_Id__c,
                ContactId,
                Contact.Phone,
                LastModifiedDate,
                LeadId,
                Status
            from CampaignMember
            where CampaignId = :this.camp.Id and
                Status != ''
        ];      

        Set<Id> leadIds = new Set<Id>();

        for (CampaignMember member: this.campaignMembers) {
            leadIds.add(member.LeadId);
        }

        leadIds.remove(null);

        this.leads = new List<Lead>();

        if (!leadIds.isEmpty()) {
            this.leads = [
                select Email,
                    FirstName,
                    LastName,
                    MobilePhone,
                    Name,
                    Phone,
                    (
                        select Subject,
                            LastModifiedDate
                        from ActivityHistories
                        where lastModifiedDate != null and
                            Subject != null
                        order by LastModifiedDate desc
                        limit 1
                    )
                from Lead
                where Id in :leadIds
            ];
        }
    }
}

You could probably reduce this down into one query and then build a list of leads off the queried items, but I'm not sure it's worth the headache and possible future confusion to save a single query in the controller.

NOTE: This code has not been tested and may contain typographical or logical errors.
RajashriRajashri
Thanks..I got the error that

Illegal assignment from LIST<CampaignMember> to LIST<CampaignMember> at line 19 column 9

How can i resolve that error?
pconpcon
I don't know what to tell you.  I just saved the test to a new class in my Sandbox without any issues.  What API version are you using for the controller?
RajashriRajashri
I am using API 32.0...What should i use?
pconpcon
That is the API version I used, without issue.  How are you deploying the code?
RajashriRajashri
I am using the personal development environment..
what changes i need to do to get this code work there..

 
pconpcon
If you wholesale delete all of your code in the class and replace it with the code above, do you still get the Illegal Assignment error?
RajashriRajashri
Thanks a lot..I am still getting Illegal Assignment  error in my personal Dev environment but code is getting saved successfully on sandbox...

Also i am now trying to print the values in sandbox VF page...but getting error that  
Error: Invalid field CampaignMembers for SObject CampaignMember
can you please help ?
Below is my VF page...

<apex:page StandardController="Campaign"  extensions="CampaignMemController">
<apex:form >
        <apex:pageBlock title="Campaign Members Details" mode="maindetail">
                       <apex:pageBlockSection title="Campaign Members"  id="cm3">
                <apex:pageblocktable value="{!CampaignMembers}" var="cm">
                          <apex:column >
                               <apex:pageblocktable value="{!cm.CampaignMembers}" var="cpm">
                                        <apex:column headerValue="Name">
                                 <apex:outputfield value="{!cpm.LeadID}" rendered="{!cpm.LeadID != null}"/>
                                  </apex:column>
                               </apex:pageblocktable>
                </apex:column>
                                  <apex:column >
                                <apex:pageblocktable value="{!cm.ActivityHistories}" var="tm">
                                    <apex:column headerValue="Subject">
                                        <apex:outputfield value="{!tm.Subject}"  rendered="{!tm.Subject != null}"/>
                                                                          </apex:column>
                                </apex:pageblocktable>
                           </apex:column>
            </apex:pageblocktable>
                         </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
 
 
pconpcon
This is because we have changed how the backing data is stored.  What data are you trying to display?  The CapaignMembers, the Leads and the ActivityHistory subjects? If these are all different tables then you can do the following:
 
<apex:page StandardController="Campaign"  extensions="CampaignMemController">
    <apex:form>
        <apex:pageBlock title="Campaign Members Details" mode="maindetail">
            <apex:pageBlockSection title="Campaign Members"  id="cm3">
                <apex:pageblocktable value="{!Leads}" var="lead">
                        <apex:column headerValue="Name">
                            <apex:outputfield value="{!lead.Id}" />
                        </apex:column>
                    </apex:column>
                    <apex:column>
                        <apex:pageblocktable value="{!lead.ActivityHistories}" var="history">
                            <apex:column headerValue="Subject">
                                <apex:outputfield value="{!history.Subject}"  rendered="{!history.Subject != null}"/>
                            </apex:column>
                        </apex:pageblocktable>
                    </apex:column>
                </apex:pageblocktable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

If you need the data presented differently then you will need to make a custom data structure to map the data how you want.
RajashriRajashri
Thanks! I am trying to display 
The CapaignMembers, the Leads and the ActivityHistory data ...from the above controller to visual force page but it is not showing me any data...
RajashriRajashri
Also ..How can i print the details of 
The CapaignMembers, the Leads and the ActivityHistory data ...from the above controller to visual force page