Search code examples
phpsalesforcesoql

Salesforce SOQL - find account by contact email


I am using PHP and SOQL to query Salesforce.

I am trying to find the account by the email address of one of the contacts.

So far I have this:

SELECT Id, Name, (SELECT Id, Name FROM Contacts WHERE Email = '[email protected]')
FROM Account

But it produces the entire list of accounts, with an empty third column except (presumably - the list is too large) where there is in fact a match.

    Id                  Name                Contacts
1   xxxxxxxxxxxxxxxxxx  Jane Doe    
2   yyyyyyyyyyyyyyyyyy  Richard Roe
3   zzzzzzzzzzzzzzzzzz  Mortimer Snerd  

I have gone round and round trying to figure out how to do this without real JOINs - can someone please point me in the right direction?


Solution

  • You need to query in this manner:

    SELECT Id, Name
    FROM Account
    WHERE Id IN (SELECT AccountId FROM Contacts WHERE Email = '[email protected]')