Salesforce Apex-从SOQL查询填充对象 [英] Salesforce Apex - Populating an object from SOQL query

查看:259
本文介绍了Salesforce Apex-从SOQL查询填充对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的SOQL查询,该查询返回与Contact和CampaignMember有关的信息.我正在尝试使用SOQL查询的结果填充自定义对象.但是,加载Visualforce页面时出现以下错误:

I have a simple SOQL query that returns information relating to a Contact and CampaignMember. I'm attempting to populate a custom object with the results of the SOQL query. However I get the following error when loading the Visualforce page:

CampaignMember的字段field.name无效

List campaignMembers = [select campaign.name, contact.id,contact.firstname, contact.lastname, status, campaignId from CampaignMember where contactId = '003U000000U0eNq' and  campaignId in :campaigns];

for (Integer i = 0; i < campaignMembers.size(); i++) {
    results.add(new CampaignMemberResult(
        (String)campaignMembers[i].get('CampaignId'), 
        (String)campaignMembers[i].get('campaign.name'),
        true
    ));
}

我已经在开发人员控制台中单独运行过SOQL查询,并且查询成功.为什么我不能在for循环中从SOQL查询中提取campaign.name?

I've ran the SOQL query seperately in the Developer Console and it queries successfully. Why can I not pull in the campaign.name from the SOQL query within the for loop?

推荐答案

您看到的错误是由于您应将其写为campaignMembers[i].Campaign.Name而引起的.或者,如果您坚持使用getter语法,请 campaignMembers[i].getSobject('Campaign').get('Name') .

The error you see is caused by the fact you should write it as campaignMembers[i].Campaign.Name. Or if you insist on the getter syntax, campaignMembers[i].getSobject('Campaign').get('Name').

您是否需要包装对象(或任何CampaignMemberResult)有什么特殊的原因? 我感到很奇怪,您正在编写太多代码来实现简单的操作;)campaignMembers[i].Campaign.Name的语法也意味着您不必对字符串使用强制转换.

Any special reason you need the wrapper object (or whatever CampaignMemberResult is)? I have strange feeling you're writing too much code to achieve something simple ;) The syntax with campaignMembers[i].Campaign.Name will also mean you don't have to use casts to String.

加-如果您需要了解"此联系发生在哪些广告系列",则有两种方法:

Plus - if you need to know "in which campaigns does this Contact occur" you have 2 ways:

扁平

select contact.id,contact.firstname, contact.lastname, 
    campaignid, campaign.name, 
    status 
from CampaignMember 
where contactId = '003U000000U0eNq'

子查询

从联系人开始,您将转到相关的广告系列成员列表,然后转到广告系列以获取其名称

From contact you go down to the related list of campaignmembers, then up to campaigns to get their names

SELECT Id, FirstName, LastName,
    (SELECT CampaignId, Campaign.Name FROM CampaignMembers)
FROM Contact WHERE Id = '003U000000U0eNq'

示例如何直接在visualforce中使用"flat"结果(不使用CampaignMemberResult):

Example how to use "flat" result straight in visualforce (without CampaignMemberResult):

顶点:

public List<CampaignMember> flatMembers {get;set;} // insert dick joke here

flatMembers = [select contact.id,contact.firstname, contact.lastname, 
    campaignid, campaign.name, 
    status 
from CampaignMember 
where contactId = '003U000000U0eNq'];

VF:

<apex:pageBlockTable value="{!flatMembers}" var="cm">
    <apex:column value="{!cm.Contact.LastName}" />
    <apex:column value="{!cm.Status}" />
    <apex:column value="{!cm.Campaign.Name}" />


编辑

我的最终目标是要在联系人上显示的Visualforce页面 记录显示所有活动的列表,每个活动旁边都有一个复选框 指示联系人是否是成员.

My end goal is a Visualforce page to be displayed on the contact record showing a list of all campaigns with a checkbox alongside each indicating if the contact is a member or not.

您确实意识到它可以迅速成长为一张长桌子吗?可能对广告系列进行了一些过滤(如果您想了解某事,请参阅"StandardSetController"的文档).另外,我敢肯定,有一些方法可以从广告系列"报告中向广告系列"添加联系人/线索-也许开箱即用的东西可以节省您的时间,并且更易于维护...

You do realize it can quickly grow into a pretty long table? Maybe some filter on campaigns (if you feel like reading about sth advanced - check the documentation for "StandardSetController"). Also I'm pretty sure there are some ways to add Contacts/Leads to Campaigns from Campaign reports - maybe something out of the box would save your time and be more maintainable...

但是代码解决方案将非常简单,从一个辅助包装器类开始:

But code solution would be pretty straightforward, start with a helper wrapper class:

public class CampaignWrapper{
    public Boolean selected {get;set;}
    public Campaign c {get; private set;}
    public CampaignWrapper(Campaign c){
        this.c = c;
        selected = !c.CampaignMembers.isEmpty();
    }
}

然后查询并构建包装器列表:

Then a query and build the list of wrappers:

List<CampaignWrapper> wrappers = new List<CampaignWrapper>();
for(Campaign c : [SELECT Id, Name, (SELECT Id FROM CampaignMember WHERE ContactId = '...')
    FROM Campaign
    LIMIT 1000]){
    wrappers.add(new CampaignMember(c));
}

您应该准备就绪;)如果只是为了显示-您甚至可能不需要wrapper类(visualforce表达式中的一些技巧甚至可以使用Map<Campaign, Boolean>……

You should be all set ;) If it's just for displaying - you might not even need the wrapper class (some tricks in visualforce expressions maybe or use Map<Campaign, Boolean> even...

1000条记录是传递给Visualforce的集合的限制(如果您的页面为只读,则为10K).除此之外-分页,最有可能与上述StandardSetController一起使用.

1000 records is the limit of collections passed to Visualforce (10K if your page will be readonly). Past that - pagination, most likely with use with abovementioned StandardSetController.

这篇关于Salesforce Apex-从SOQL查询填充对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆