3

Salesforce 架构中有两个表

               +--------------+      +-------------+
               |   Case       |      |   User      |
               |--------------|      |-------------|
               | CaseNumber   |      |id           |
               | ContactId    |      |OwnerId      |
               | IsClosed     |      |UserName     |
               +--------------+      +-------------+

ContactId 是映射到 OwnerId 的外键(注意 foo 在两个表中的位置?)

  +-------------------------------------+        +----------------------------+
  |                     Case            |        |             User           |
  |-------------------------------------|        |----------------------------|
  |CaseNumber |  ContactId |  IsClosed  |        | id | OwnerId | UserName    |
  |                                     |        |                            |
  |1             foo          false     |        | 42   foo       bob         |
  |2             bar          true      |        | 99   bar       joe         |
  |3             foobar       false     |        | 10   foobar    sally       |
  |                                     |        |                            |
  +-------------------------------------+        +----------------------------+

我想显示案例和用户之间的关系。(例如,bob 有案例 1,joe 有案例 2 等等。)我相信这被称为Left Inner Join。如果我错了,请纠正我。

+-------------------------------------+
|                                     |
|  Case.CaseNumber | User.UserName    |
|                                     |
|     1               bob             |
|     2               joe             |
|     3               sally           |
|                                     |
+-------------------------------------+

这是我认为 SOQL 查询的外观

SELECT Case.CaseNumber, User.OwnerId
FROM Case
WHERE Case.ContactId IN (SELECT User.OwnerId FROM User)

我正在关注 Salesfoce SOQL 文档 http://wiki.developerforce.com/page/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com中的示例

查询 CaseNumber 和 UserName 的正确语法是什么?

4

3 回答 3

2

我不确定我是否在架构上关注你。但是,此 SOQL 应该可以满足您的需求。

SELECT CaseNumber, Owner.Name FROM Case

这是一个更通用的链接,应该有助于编写这样的查询

http://www.salesforce.com/us/developer/docs/soql_sosl/

于 2013-08-13T19:11:08.260 回答
1

每个联系人的案例:

List<Contact> contacts = [SELECT Id, LastName, (SELECT ID FROM Cases) FROM Contact];
System.debug(contacts[0].Cases);       //list of cases for first contact
if(contacts[0].Cases.isEmpty() == false)
{
      System.debug(contacts[0].Cases[0].CaseNumber);  //case number of first case of first contact
}

每个用户的案例:

List<User> users = [SELECT Id, LastName, (SELECT ID FROM Cases) FROM User];
System.debug(users[0].Cases);    //list of cases for the first user
if(users[0].Cases.isEmpty() == false)
{
     System.debug(users[0].Cases[0].CaseNumber);  //case number of first case for first user
}

其他查询:仅限已打开病例的联系人

List<Contact> contacts = [SELECT Id, LastName, (SELECT ID FROM Cases WHERE CloseDate = null) FROM Contact WHERE Id IN (SELECT ContactId FROM Cases WHERE CloseDate = null)];

我希望这会有所帮助

于 2013-08-15T05:47:32.730 回答
0

我看到他们都准备好帮助你了,但我仍然想补充一些东西......

您始终可以使用 :workbench 站点

https://workbench.developerforce.com/login.php?startUrl=%2Fquery.php

或 force.com 测试您的查询

于 2014-12-17T14:28:27.113 回答