Beatbox: How do I add OR function to WHERE clause when pulling data from SFDC?

578 views Asked by At

Currently I am querying data from the Account object for only accounts with Opportunities. However, I want to add an OR function so that it will query data for accounts that have Contacts as well.

What is the syntax for adding an OR to that? As in (SELECT AccountID FROM Opportunity OR Contact). So if the AccountID is in either set of data, it will pull the website for me.

Current State:

query_result = svc.query("SELECT ID,Website FROM Account where ID in (SELECT accountId FROM Opportunity)")
2

There are 2 answers

0
hynekcer On

This is a restriction of SOQL Comparison Operators - Semi-Joins with IN ... that

  • You cannot use subqueries with OR.

You can get an error message

MALFORMED_QUERY Semi join sub-selects are not allowed with the 'OR' operator

even if you combine WHERE Id IN (SELECT ...) OR ... with any trivial condition. There is no solution by SOQL, but there are two complicated workarounds: A) Create two roll-up summary fields with count of Opportunities and count of Contacts to can combine them without semi-join or B) combine the results of two queries by Python.