function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Robert RobinsonRobert Robinson 

Creating a related list from 2 unrelated Objects (and a junction Object)

I have two Objects (Deal__c and Property__c) that I would like to join in a related list. Deal and Property are not related to each other but they do have a junction object Deal_Property__c (Deal is the master of Deal Property, and Deal Property has Property as a lookup field). I want to create a controller extension that for Deal A lists Properties 1, 2 and 3. I think this is possible but I cannot come up with the correct syntax. I've searched the documentation and Trailhead, but cannot quite find what I am looking for. Any ideas? Thanks.
Best Answer chosen by Robert Robinson
Robert RobinsonRobert Robinson
Got it. Solved it by working from the inside out:
SELECT Name, Id,
      (SELECT Property__c, Property__r.Name, Property__r.Asset_Type__c, Property__r.Portfolio__c, Property__r.Campus_Type__c,     Property__r.Market__r.Name 
         FROM Deal_Properties__r)
FROM Deal__c
WHERE Id = 'a06o000000EaLsT'

BIG thanks to David Liu and his "Apex Academy" for providing the framework for the solution.

All Answers

Robert RobinsonRobert Robinson
I have tried something else: I have created a lookup field (Deal_to_Link) on the Property object. My query now looks like:
Select Name, Asset_Type__c, Portfolio__c, Campus_Type__c, Market__r.name FROM Property__c Where Deal_r.Id= Deal_to_Link__c
This query is throwing an "Unknown error". No sure what i am missing.
Robert RobinsonRobert Robinson
Got it. Solved it by working from the inside out:
SELECT Name, Id,
      (SELECT Property__c, Property__r.Name, Property__r.Asset_Type__c, Property__r.Portfolio__c, Property__r.Campus_Type__c,     Property__r.Market__r.Name 
         FROM Deal_Properties__r)
FROM Deal__c
WHERE Id = 'a06o000000EaLsT'

BIG thanks to David Liu and his "Apex Academy" for providing the framework for the solution.
This was selected as the best answer