带子查询的 SOQL 查询

发布于 2024-09-29 08:52:50 字数 1737 浏览 5 评论 0原文

我无法从 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...

  1. 为什么子查询没有返回任何 CampaignMember 对象?
  2. 为什么没有过滤联系人列表? (嗯,显然 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...

  1. Why aren't any CampaignMember objects being returned from the subquery?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-10-06 08:52:50

您可以对联系人使用半连接来将联系人过滤到您想要的集合,类似这样的

[select id, firstname, lastname, 
     (select id, status, comment__c, campaignid from CampaignMembers)
     from contact where id in 
          (select contactId from campaignMember where campaignId in :cams];

另一种选择是从campaignMmeber 驱动。

[select contact.id, contact.firstname, contact.lastname, 
  status, comment__c, campaignId from campaignMembers 
  where contactId !='' and  campaignId in :cams];

You could use a semi-join on contacts to filter the contacts to the set you want, something like this

[select id, firstname, lastname, 
     (select id, status, comment__c, campaignid from CampaignMembers)
     from contact where id in 
          (select contactId from campaignMember where campaignId in :cams];

Another option would be to drive from campaignMmeber instead.

[select contact.id, contact.firstname, contact.lastname, 
  status, comment__c, campaignId from campaignMembers 
  where contactId !='' and  campaignId in :cams];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文