Apex代码 - 如何从Subquery获取广告系列成员的数据

时间:2014-11-14 06:40:11

标签: salesforce apex-code visualforce soql

我正在尝试在我的VF页面上显示四个字段名称,来自(Campaign成员)的状态和主题以及(活动历史记录)Object中的上次修改日期。我无法从Campaign成员中获取名称和状态字段。

任何人都可以告诉我如何从Campaign Members获取Name和Status字段.Below是我的Controller代码。

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>();
        getCampaignMembers();
        startDate = null;
        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) {
                for (ActivityHistory ah : ld.getSObjects('ActivityHistories')) {
                    lMemberWrappers.add(new MemberWrapper(ld.Name, ah.Subject, ah.LastModifiedDate, ld.CampaignMembers.get(0).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 Object Status {get; set;}
        public String Name {get; set;}
        public String Subject {get; set;}
        public Datetime LastActivityHistory {get; set;}
        public MemberWrapper(String Name, String Subject, Datetime LastActivityHistory, Object Status  ) {
            this.Name = Name;
            this.Subject = Subject;
            this.LastActivityHistory = LastActivityHistory;
            this.Status = Status;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

由于ActivityHistory要求,这将带您多个查询。这里有四个层次的关系:广告系列 - &gt; CampaignMember - &gt;铅 - &gt; ActivityHistory,因此您不能仅使用子查询来执行此操作。

以下代码可为您提供所需的信息:

Set<Id> leadIds = new Set<Id>();
List<Campaign> campaignList = [Select ID, (SELECT Id, Status, LeadId FROM CampaignMembers) FROM campaign WHERE id = :camp.Id ];

for (Campaign c : campaignList) {
    for (CampaignMember cm : c.CampaignMembers) {
        leadIds.add(cm.LeadId);
    }
}

Map<Id, Lead> leadMap = new Map<Id, Lead>([Select Id, Name, (Select Subject, LastModifiedDate from ActivityHistories Order By LastModifiedDate DESC limit 1) From Lead Where Id IN :leadIds]);
for (Campaign c : campaignList) {
    for (CampaignMember cm : c.CampaignMembers) {
        Lead ld = leadMap.get(cm.LeadId);
        if (ld.ActivityHistories.size() > 0) {
            ActivityHistory ah = ld.ActivityHistories[0];
            lMemberWrappers.add(new MemberWrapper(ld.Name, ah.Subject, ah.LastModifiedDate, cm.Status));                
        }
    }
}

如果没有活动,我只是跳过创建MemberWrapper,但如果你愿意的话,你可以把它改成只放空白值。