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

Get the data from SubQuery

I am trying to retrieve the Status value from the SubQuery and wanted to add that value in my Wrapper List.Can anyone please tell me how to retrieve the Status Value from Subquery.Status field is available in Campaign Members.

Below is my controller code

public with sharing class CampaignView {
public Campaign camp {get; set; }
    public List<MemberWrapper> lMemberWrappers {get; set;}
    public DateTime startDate{get;set;}  
    public CampaignView(ApexPages.StandardController controller) {
        camp = (Campaign)controller.getRecord();
        lMemberWrappers = new List<MemberWrapper>();
              for(Lead ld : CampaignMembers) {
              for (ActivityHistory ah : ld.getSObjects('ActivityHistories')) {
            lMemberWrappers.add(new MemberWrapper( ld.Name,cm.Status));
       private List<Lead> CampaignMembers;
    public List<Lead> getCampaignMembers() {
        CampaignMembers = [Select Id,Name, Phone,MobilePhone,Email, LastModifiedDate,(Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,Lead.Name,LeadID,ContactID,Lead.Phone,Lead.Email, Lastmodifieddate,Lead.LastmodifiedDate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone  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 ) ];
        return CampaignMembers;
    public class MemberWrapper {
        public String Status {get; set;}
        public String Name {get; set;}
        public MemberWrapper(String Name,String Status ) {
            this.Name = Name;
                 this.Status = Status;
praveen murugesanpraveen murugesan
Hi Raja,

Hope this example will help you.
List<Account> acct = new List<Account>();

String sql = 'SELECT Name,Phone,External_ID__c,Type,Industry,AccountNumber,Description,FAX,SLA__c,Rating,(SELECT Name,StageName,CloseDate,Probability FROM Opportunities) FROM Account WHERE ID=:localId';
acct = Database.Query(sql);
        for(Account ac:acct)
            List<Opportunity> opptys = new List<Opportunity>();
            opptys = ac.getSObjects('Opportunities');
            for(Opportunity opt:opptys)

Mark this as best answer if its helps.

Thanks Prveen..I am using  ld.getSObject('CampaignMembers').get('Status')  to pickup the status from subquery  and adding to the list

lMemberWrappers.add(new MemberWrapper( ld.Name,ld.getSObject('CampaignMembers').get('Status') ));

but it's giving me an error that  " Invalid relationship CampaignMembers for Lead" 

Do you have any Idea?