+ Start a Discussion
Craig JonesCraig Jones 

Getting data from multiple objects through Junction

I have 2 object that are related to each other, via a Junction object, and i want to get data from them both in one query.

 

My set up is:

 

Product__c: name, description

 

JuncProdToAccount: Product__c, Account (these two links are master-detail relationships)

 

Account (standard salesforce account object)

 

I am wanting to write some SOQL that will get all Products... and if there is a link to the Junction table, return the Account.Name of the linked Account.

 

Is this possible in one query?

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
This should help:

Select Id, (Select Account__r.Name from JuncObjects__r) from Product

All Answers

Bhawani SharmaBhawani Sharma
Select FirstParent__r.Id, SecondParent__r.Id from JuncObject__c

is this what you are talking about?
Craig JonesCraig Jones
In a way... but that wouldn't give me the Products with no link to an
account.

My thinking was to go the query against the product table, that way i can
get all the products, then see which ones link to an account through the
junction table and get the account names from the ones that do.

Hope that makes sense
Bhawani SharmaBhawani Sharma
This should help:

Select Id, (Select Account__r.Name from JuncObjects__r) from Product
This was selected as the best answer
Craig JonesCraig Jones
Yeah, that helps a lot thanks! Just what i was after!

One last thing you may be able to help me with :)... how will i think be
able to access that field within a visualforce page? What would be its
variable name?