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

How to get the data from Inner Query

 i am trying to add Phone and Status field from Campaign Members Object to my VF page.How can i modify my for loop if i want to print Phone and Status field from campaign Members.

Below is my for loop

 for(Campaign c :[Select ID,(SELECT Id,CampaignId,Status  FROM CampaignMembers where CampaignId =:camp.Id) FROM campaign WHERE id=:camp.Id ]){
              for(Lead ld : CampaignMembers) {
                         if ((ld.ActivityHistories.size() == 0) || ld.ActivityHistories.size() == null) {
                        lMemberWrappers.add(new MemberWrapper(ld.Phone,ld.CampaignMembers.get(0).Status));

and SOQL Query is

CampaignMembers= [Select Id, Phone,(Select id, Status From CampaignMembers where CampaignId =:camp.Id  ),
        (Select Subject, Id,lastModifiedDate From ActivityHistories    order by LastModifiedDate DESC  LIMIT 1  )
        From Lead  where Id IN(select LeadId from campaignMember where campaignId =:camp.Id ) ];

I am also getting relation ship error during the fetching lead as in inner query
Amritesh SahuAmritesh Sahu
Hi Rajashri,

Try changing the line
 for(Lead ld : CampaignMembers)
 for(Lead ld : c.CampaignMembers)

we can't use for (Lead ld : c.CampaignMembers) as it's giving an error that 

Loop variable must be an SObject or list of CampaignMember
Akshay DeshmukhAkshay Deshmukh

I think your outside query is wrong. You are comparing campaign Id twice. Also, inside for loop you are trying storing Id to Lead. it should be CampaignMember.
ID is special keyword, dont use it to store records.

Try with following query, your problem should be solved. I have tested the query, it is working fine.

 for(Campaign c :[Select ID,(SELECT Id,CampaignId,Status  FROM CampaignMembers ) FROM campaign WHERE id=:camp.Id ]){
    //your campaign code goes here
     for(CampaignMember cmber : c.CampaignMembers){
             //your campaignMember  code goes here
Thanks Akshay.I tried that also now it's giving an error that
Loop variable must be an SObject or list of CampaignMember 
Akshay DeshmukhAkshay Deshmukh
can you post your debug log and Code?
Akshay DeshmukhAkshay Deshmukh
Can you post your complete code.