Given an Account name such as "Vegan Delight" should I get the ID of the Account first? Do I then do:

SELECT Id, Name from Opportunity WHERE AccountId='oio23094'

or is there a way to look this up directly, using the string "Vegan Delight"? I know nothing of JOINs on Salesforce.

I am using the API, version 34. This is from a Connected app.


You need to filter on Account Name and not Account Id. So just change your WHERE clause to be WHERE Account.Name = 'Vegan Delight' for an exact match, or you can use LIKE pattern maching by using the LIKE keyword - WHERE Account.Name LIKE '%Vegan' (where the account name starts with Vegan). Check this out

  • 1
    technically not a 'fuzzy match' ; LIKE matches patterns; fuzzy matching finds hits based on mathematical "nearness" algorithms. Would be nice if SFDC search were as good as Google search – cropredy Dec 4 '15 at 1:39
  • Correct, I should've used a different term. Updated the answer. – Boris Bachovski Dec 4 '15 at 1:43

You can do this:

SELECT Id, Name from Opportunity WHERE Account.Name like '%VEGAN DELIGHT%'

While Boris's answer is the simplest and usually the best approach, alternately you can query on the Account and use the relationship queries as per the docs: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_using.htm

You could do a query such as:

SELECT Id, (SELECT Id, Name FROM Opportunities) FROM Account WHERE Account.Name = 'Vegan Delight'

A benefit of doing it this way is that you end up with a list of Opportunities related to each Account returned from the results. This may be handy if you're expecting results from multiple Accounts and you want the Opportunities grouped together without having to do this yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.