带子查询的 SOQL 查询
我无法从 Salesforce/Apex/SOQL 查询中获取所需的结果。
我想要: 联系人对象列表,仅包含作为一组营销活动的 CampaignMembers 的联系人;他们应该可以轻松访问该营销活动成员的数据。 (我的最终目标是创建一个 VF 页面,其中包含与这些营销活动相关的所有联系人的列表,并用网格指示每个营销活动的状态。)
这些工作:
Campaign[] cams = [SELECT id, name
FROM Campaign
WHERE parentid = '70170000000LRIe'];
System.debug(cams);
// returns ~4 Campaign objects
CampaignMember[] cmembers = [SELECT id, status, contactid, campaignid
FROM CampaignMember
WHERE campaignid in :cams];
System.debug(cmembers);
// returns about 40 CampaignMember objects.
这是我的问题:
Contact[] members = [SELECT id, firstname, lastname,
(SELECT id, status, comment__c, campaignid
FROM Contact.CampaignMembers
WHERE campaignid in :cams)
FROM Contact];
System.debug(members);
// contains ALL Contacts in the DB, but I wanted filtered results.
System.debug(members[x].CampaignMembers);
// this is a contact I've verified has a qualifying CampaignMember, but the list is empty.
// UPDATE: CampaignMembers are now being returned, not sure what changed...
为什么子查询没有返回任何 CampaignMember 对象?- 为什么没有过滤联系人列表? (嗯,显然 b/c 中没有 WHERE 子句,但是 WHERE 子句提供了我想要的内容?)
我知道我可以通过单独执行 CampaignMember 查询并循环遍历它来准备联系人来做到这一点查询,但是当子查询应该工作时,这似乎需要很多额外的处理。
谢谢!
更新
CampaignMember 对象现在出现了 - 奇怪的是 - 我一定是在没有注意到的情况下修复了一些小拼写错误(是的,它们返回了多个列,这似乎没问题)。
不过,我仍然不知道如何过滤联系人查询......
I'm having trouble getting the results I want from a Salesforce/Apex/SOQL query.
I want: A list of Contact objects containing only contacts who are CampaignMembers of a set of campaigns; and they should have the data from that Campaign member easily accessible.
(my eventual goal is a VF page with a list of all Contacts connected to any of these campaigns with a grid indicating their status for each campaign.)
These work:
Campaign[] cams = [SELECT id, name
FROM Campaign
WHERE parentid = '70170000000LRIe'];
System.debug(cams);
// returns ~4 Campaign objects
CampaignMember[] cmembers = [SELECT id, status, contactid, campaignid
FROM CampaignMember
WHERE campaignid in :cams];
System.debug(cmembers);
// returns about 40 CampaignMember objects.
Here's my problem:
Contact[] members = [SELECT id, firstname, lastname,
(SELECT id, status, comment__c, campaignid
FROM Contact.CampaignMembers
WHERE campaignid in :cams)
FROM Contact];
System.debug(members);
// contains ALL Contacts in the DB, but I wanted filtered results.
System.debug(members[x].CampaignMembers);
// this is a contact I've verified has a qualifying CampaignMember, but the list is empty.
// UPDATE: CampaignMembers are now being returned, not sure what changed...
Why aren't any CampaignMember objects being returned from the subquery?- Why isn't the Contact list being filtered? (well, obviously b/c there's no WHERE clause in it, but what WHERE clause provides what I want?)
I know I could do this by doing the CampaignMember query on its own and looping through it to prep a Contact query, but that seems like a lot of extra processing when a subquery should work.
Thanks!
Update
The CampaignMember objects are now showing up - oddly - I must have fixed some small typo without noticing (and yes, they're returning multiple columns and that seems to be fine).
I still can't figure out how to filter the Contact query, though...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以对联系人使用半连接来将联系人过滤到您想要的集合,类似这样的
另一种选择是从campaignMmeber 驱动。
You could use a semi-join on contacts to filter the contacts to the set you want, something like this
Another option would be to drive from campaignMmeber instead.