I have 3 objects

Object 1 and Object 3 are Connected through a Junction Object Object 2

Obj1 Record                      Obj2 Record                 Obj3 Record  Rate
1234                             456                         1111          $10
5555                             678                         1111          $40 

I need to print as 1234 has the rate $10 and 5555 has the rate $40 but there is no direct relation between object 1 and object 3

I tried storing the Obj1 Record Id in Obj 3 and tried to use in the page block table but it is getting overwritten.


Since Obj2 is the many-to-many relationship, query from there first:

SELECT Obj1.Name, Name, Obj3.Name, Obj.Rate__c FROM Obj2

In your pageBlockTable, you can output values via relationships:

<apex:column value="{!row.Obj1.Name}" />
<apex:column value="{!row.Name}" />
<apex:column value="{!row.Obj3.Name}" />
<apex:column value="{!row.Obj3.Rate__c}" />

Adjust the names to suit your fields/objects.


Please excuse the answer as not having enough reputation to post as comment.

Okay, a quick question, as there is a junction object involved, wouldn't it be the case that Obj1 will have multiple relationships with Obj3. If not, wouldn't a simple lookup will be better.

Having said so (and assuming that I am wrong and you do opt for junction and fetch all the multiple rates), you can use SOQL query on junction object to get all the information from Obj1 and Obj2 as:

Select Id, Obj1.Name, Obj3.Name, Obj.Rate From Obj2 Where {Replace with Conditions} 

And store it in a map which has a mapping with a list.

Almost forgot, you will also need to store it in a list to iterate over on page side and show these values.

More details on queries here: https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html

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.