Salesforce - Apex - 根据事件历史记录查询帐户

标签 salesforce subquery soql apex

嘿 Salesforce 专家,

我有一个如何高效查询账户信息的问题。我想根据 ActivityHistory 对象中的更新查询帐户。我遇到的问题是,无论是否有“完整”的 activeHistory ,所有帐户都会被检索。那么,有没有一种方法可以编写此查询来仅检索具有 status="complete"且 Type_for_reporting='QRC' 的 activeHistory 帐户?

List<Account> AccountsWithActivityHistories = [    
    SELECT
         Id
        ,Name
        ,( SELECT
                ActivityDate
               ,ActivityType
               ,Type_for_Reporting__c
               ,Description
               ,CreatedBy.Name
               ,Status
               ,WhatId
            FROM ActivityHistories
            WHERE Status ='complete'  and Type_for_Reporting__c = 'QRC'
        )
    FROM Account
];

最佳答案

您在历史记录中有一个 WHERE 子句,但在帐户级别仍然缺少一个。

例如,这将仅返回具有联系人的帐户:

SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact) // try with NOT IN too

对于 Activity,情况会比较棘手,因为它们不喜欢以这种方式在 WHERE 中使用。

http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select.htm

The following objects are not currently supported in subqueries:

  • ActivityHistory
  • Attachments
  • Event
  • EventAttendee
  • Note
  • OpenActivity
  • Tags (AccountTag, ContactTag, and all other tag objects)
  • Task

此外ActivityHistory底部的小字定义也有点令人沮丧。

对没有“查看所有数据”权限的用户的以下限制有助于防止性能问题:

  • In the main clause of the relationship query, you can reference only one record. For example, you can’t filter on all records where the account name starts with ‘A’; instead, you must reference a single account record.
  • You can’t use WHERE clauses.
  • You must specify a limit of 499 or fewer on the number of rows returned in the list.
  • You must sort on ActivityDate in ascending order and LastModifiedDate in descending order; you can display nulls last. For example: ORDER BY ActivityDate ASC NULLS LAST, LastModifiedDate DESC.

看起来您将需要多个查询。转到任务(或事件,具体取决于自定义字段可见),组成一组 AccountId,然后查询帐户?

或者您可以从原始查询中手动筛选列表,将帐户复制到帮助程序列表:

List<Account> finalResults = new List<Account>();
for(Account a : [SELECT...]){
    if(!a.ActivityHistories.isEmpty()){
        finalResults.add(a);
    }
}

关于Salesforce - Apex - 根据事件历史记录查询帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21059620/

相关文章:

excel - 我可以在一项 Talend 工作中使用两次 excel 文件吗?

mysql - 优化子查询选择每组最后一条记录

javascript - 合并来自不同查询的数据,不重复

php - MySQL 一对一关系优化

salesforce - SOQL:访问联系人所有者字段

mysql - SOQL:避免使用 NULL 结果计数,而是返回 0(零)

c# - 使用 REST API 从 Salesforce.com 调用 "Get Updated"记录时出现问题

salesforce - 在没有包的情况下从开发组织部署到新的不相关组织

python - 如何通过 simple_salesforce 进行 Salesforce 批量 API 调用?

mysql - 计算mysql表的有限行数之和?