简体   繁体   English

在 Python 中加入 SOQL

[英]JOIN with SOQL in Python

Issue: Using the documentation linked below, I tried inner joining two SOQL tables together without success.问题:使用下面链接的文档,我尝试将两个 SOQL 表内部连接在一起,但没有成功。 Can anyone help me figure out what's wrong?谁能帮我找出问题所在? Assuming it has to do with the syntax (ie parenthesis).假设它与语法(即括号)有关。 Thanks in advance!提前致谢!

Documentation for Joins:连接文档:

https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.ZFC35FDC70D5FC69A769883A82E2

Python SOQL Code: Python SOQL 代码:

records =   sf.query_all('SELECT Event__c.Id, Event__c.Supplier__c,' +
                         ' Event__c.Promo_Models__c, Event__c.Lead_Promo_Model_Name__c,' +
                         ' Event__c.Event_Date__c, Consumer_Feedback__c,' +
                         ' (SELECT Contact.Gender__c, Contact.Ethnicity__c, Contact.CreatedDate FROM Contact)' +
                         ' FROM Event__c' +
                         ' WHERE Event__c.Lead_Promo_Model__c IN (SELECT Contact.Id FROM Contact)' +
                         ' AND Supplier__c LIKE \'ABCCompany%\'' +
                         ' AND Event_Date__c > 2019-06-30' + 
                         ' AND Event_Date__c < 2020-07-01', include_deleted=False)

df_sf = pd.DataFrame(records['records'])

Error:错误:

File "", line 10 ' WHERE Event__c.Lead_Promo_Model__c IN ('SELECT Contact.Id FROM Contact')' + ^ SyntaxError: invalid syntax文件“”,第 10 行 ' WHERE Event__c.Lead_Promo_Model__c IN ('SELECT Contact.Id FROM Contact')' + ^ SyntaxError: invalid syntax

If a relationship from a builtin object to a custom object is used then you need to read an example on this documentation page Understanding Relationship Names, Custom Objects, and Custom Fields .如果使用了从内置 object 到自定义 object 的关系,那么您需要阅读此文档页面上的示例了解关系名称、自定义对象和自定义字段

You need to find the field definition of your custom field Event__c.Lead_Promo_Model__c that has a relationship to Contact (this relationship is a consequent of your WHERE condition) and read the line "Related List Label" and "Child Relationship Name".您需要找到与Contact有关系的自定义字段Event__c.Lead_Promo_Model__c的字段定义(此关系是您的 WHERE 条件的结果)并阅读“相关列表标签”和“子关系名称”行。 Both are probably "Contacts" by default, but the first can be any string and the second can be any API name.默认情况下,两者都可能是“联系人”,但第一个可以是任何字符串,第二个可以是任何 API 名称。 Then you add "__r" to the item "Child Relationship Name".然后将“__r”添加到“子关系名称”项。

You replace (SELECT Contact.Gender__c,... FROM Contact) by (SELECT Contact.Gender__c,... FROM Contact__r) .您将(SELECT Contact.Gender__c,... FROM Contact)替换为(SELECT Contact.Gender__c,... FROM Contact__r) See the Contact__r replaced .请参阅Contact__r替换 That's all.就这样。

I expect that all other parts are correct or could be easily fixed by you.我希望所有其他部分都是正确的,或者您可以轻松修复。 Nobody other knows your custom fields names.没有其他人知道您的自定义字段名称。 You can verify that everything except the nested SELECT works, then to add a subquery with native fields ... (SELECT Contact.Id FROM Contacts__r)... and finally also custom fields of Contact.您可以验证除嵌套的 SELECT 之外的所有内容是否有效,然后添加带有本机字段的子查询... (SELECT Contact.Id FROM Contacts__r)...最后还有联系人的自定义字段。

The second screenshot in the documentation is little obsoleted without a "Related List Label", but it is not important for SOQL.文档中的第二个屏幕截图在没有“相关列表标签”的情况下已经过时,但对于 SOQL 来说并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM