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

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

问题描述

我有一个简单的 SOQL 查询,可返回与联系人和 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 的字段 Campaign.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中直接使用平坦"结果(没有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));
}

你应该都准备好了 ;) 如果它只是为了显示 - 你甚至可能不需要包装类(visualforce 表达式中的一些技巧可能或者使用 Map 甚至......

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天全站免登陆