Salesforce SOQL查询:如何在WHERE条件下使用SELECT

时间:2018-10-22 13:59:04

标签: select salesforce where soql

对于给定的SOQL查询,尝试优化SOQL查询。我有两个查询

查询1 ,以从Salesforce获取给定用户帐户的用户ID

SELECT Id,Headline__c,Description__c
FROM Incident__c
WHERE Id IN (SELECT Incident__c FROM Node_Affected__c 
        WHERE (Incident__r.Account__c = '034524000000uzurX')
        AND Incident__r.Indicator__c = false)

查询2 以给定的用户ID查找自定义对象

SELECT Id,Headline__c,Description__c
FROM Incident__c
WHERE Id IN (SELECT Incident__c FROM Node_Affected__c 
        WHERE (Incident__r.Account__c = null OR Incident__r.Account__c IN (SELECT id From User WHERE Username = 'test@testmail.com'))
        AND Incident__r.Indicator__c = false)

现在,我想将其优化为一个查询,这样就不必从外部系统进行两个api调用。所以我在尝试

MALFORMED_QUERY: 
Incident__r.Account__c IN (SELECT id From User WHERE Username = 'test@testmail.com'))
                                          ^
ERROR at Row:5:Column:87
Nesting of semi join sub-selects is not supported

但是我收到以下错误

int**

有没有更好的方法来解决这类问题?在此先感谢:)

1 个答案:

答案 0 :(得分:0)

您应该能够使用SOQL功能来跟踪外键,并遵循与用户表的account__c关系,并从那里过滤用户名,例如

SELECT Id, Headline__c, Description__c
FROM Incident__c
WHERE Id IN (SELECT Incident__c FROM Node_Affected__c 
        WHERE (Incident__r.Account__c = null OR 
               Incident__r.Account__r.Username = 'test@testmail.com')
        AND Incident__r.Indicator__c = false)