Home > Enterprise >  salesforce api build query with array as parameter (IN condition instead of OR)
salesforce api build query with array as parameter (IN condition instead of OR)

Time:12-18

I have a list of emails and I have to check which of these emails already corresponds to a salesforce account and, for these, I have to get the Id to store in my system. The list is in a form of a php array and the api call is made via curl.

I'm able to perform the request if the call has all the emails in an OR condition:

$query = "query/?q="; $sql = "SELECT Id, Email FROM Account WHERE Email='[email protected]' OR Email='[email protected]'"; $url = $domain . $api . $query . $sql;

But my email list is very long so what I'd like to do is performing something like this:

"SELECT Id, Email FROM Account WHERE Email IN (" . $emails . ")"

Of course, this is SQL language, but is there a way to do this on salesforce? I tried using the same language but it doesn't work:

$sql = "SELECT Id, Email FROM Account WHERE Email IN (" . $emails . ")";

the code above returns an error like "bind variables only allowed in apex code - malformed query"

so, is there a way to perform a query with the IN condition instead of OR ?

CodePudding user response:

There's a shortcut but it's not as great as you'd hope.

You need to write your query as

SELECT Id, Email
FROM Account
WHERE Email IN ('[email protected]', '[email protected]', '[email protected]')

SOQL via API doesn't offer bind variables / prepared statements you'd expect from "normal" dialect. (It absolutely is possible and easy in Apex, the server-side language... "just" not over the API). And the query's total length has to be under 100K.

If this is unacceptable - Apex developer could expose for you a REST (or SOAP) service and you could pass the list of emails to it, get results back. It'd be limited to 50K records returned and 6 MB heap size (~RAM) consumed, whichever's lower.

CodePudding user response:

Do you know how to get these Ids using another query? You are able to make subquery inside WHERE clause. Something like this:

SELECT Id, Email FROM Account WHERE id IN (SELECT AccountId FROM Contact WHERE ...)
  • Related