Search code examples
salesforcesoql

Salesforce REST API SOQL get account for opportunies


I want to do a query for all my opportunities (or contacts or whatever) and then join in the account so I can get the name etc. I'm able to get it to work querying accounts and joining the opportunities, but not the other way around.

select Name, Id, (select Id, Name, CloseDate, StageName, Amount from Opportunities where ownerId = 'id') from Account

Is there a way to do the query on opportunities instead? I don't want to get all accounts and loop through to get the opportunity data. Should be a simple belongs to type relationship.

I've tried such queries as:

select Id, Name, CloseDate, StageName, Amount, (select Name, Id from Account) from Opportunity where ownerId = 'id'

But that gives me "Didn't understand relationship 'Account' in FROM part of query call."

Thanks!


Solution

  • For child->parent relationships you just use a dotted path to the fields you want, e.g.

    Select id, Amount, CloseDate, Account.Name from Opportunity