Relationship Queries in SOQL

Relationship queries helps you extract data from Salesforce database with minimum possible number of queries. Let me give you simple examples of all kinds of relationship queries you can write.
 
Basic Child to Parent

Select Id, Name, Account.Name From Contact

Account is the name of relationship thats defined by AccountId lookup field on contact object.

Expanded Child to Parent

Select Id, Name, Account.Parent.Name From Contact

This will retrieve the name of the parent account of the account associated with the contact

Basic Parent to Child

Select Id, Name (Select Name From Contacts) From Account

You know what this will return.

Combined Child to Parent and Parent to Child

Select Id, Name, Account.Name (Select Quantity, UnitPrice, TotalPrice, PricebookEntry.Name, PricebookEntry.Product2.Family From OpportunityLineItems) From Opportunity

Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s