I got 3 objects, BUnit__c, Row__c and Portfolio__c, with the following design

Portfolio__c(Many)-----------(1) BUnit__c (1)-----------(Many)Row__c

Portfolio__c object has a lookup to the BUnit__c, Row__c and BUnit__c are Master-detail.

I want to access a field in Portfolio__c from a Row__c object query. Can I access the Portfolio__c object through BUnit__c from Row__c object ? Or do I have to create a lookup on Row__c to Portfolio__c ?

  • is this for a trigger use case or a VF controller use case? That is, under what circumstances will you have a row (or rows) that need to fetch Portfolio? In addition, since 1 BU has many Rows and also many Portfolios, how do you known which Portfolio is the one for a given Row?
    – cropredy
    Nov 27 '15 at 18:06
set<Id> setBUIds = new  set<Id>(); //first get all the BUnit__c  ids
 for(Row__c objRow: [SELECT Id, BUnit__c  FROM Row__c ])
  setBUIds .add(objRow.BUnit__c );//add all BUnit__c  id into set
for(Portfolio__c objPortfolio: [SELECT Id, BUnit__c  FROM Portfolio__c WHERE BUnit__c IN: setBUIds ])
//now based on set of BUnit__c  ids query all the Row__c records.
  • ? OP wants to access a Portfolio__c from Row__c
    – cropredy
    Nov 27 '15 at 18:05
  • @crop1645 yes, i got a current query on Row__c, so I need to access Portfolio__c from there.
    – Tinkerbell
    Nov 30 '15 at 11:02
  • @Butterfly. check above ans.
    – Ratan Paul
    Nov 30 '15 at 11:33

You'll probably want to query for your Junction object (BUnit), and get portfolio and row fields from there, using relationship fields (__r).

You would do something like the following:

SELECT Portolio__r.PersonName, Row__r.RowValue__c FROM BUnit__c WHERE Portfolio__c = 'portfolioId'

That way you will get both Row an Portfolio information querying the BUnit table.

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.