0
<apex:pageBlockTable value="{!oldSubscriptionAndAsset}" var="assetAndSubs">
     <apex:column>
           <apex:facet name="header">Asset</apex:facet>
           <apex:outputText value="{!assetAndSubs.SBQQ__Asset__r.Name}"/>
     </apex:column>
     <apex:column>
           <apex:facet name="header">Asset</apex:facet>
           <apex:outputText value="{!assetAndSubs.SBQQ__Product__r.Name}"/>
     </apex:column> 
</apex:pageBlockTable>

oldSubscriptionAndAsset =[SELECT Id, SBQQ__Product__r.Name, SBQQ__Quantity__c,(SELECT Id, SBQQ__Asset__r.Name, SBQQ__Asset__r.SerialNumber, SBQQ__Asset__r.Is_Selected_For_Renewal__c  FROM SBQQ__SubscribedAssets__r) FROM SBQQ__Subscription__c];

I have a master detail relationship, Subscription(Master) and SubscribedAsset(Child), in my visualforce page, I want to show in different column the subscription name (master), name of the asset in lookup field Asset__c in SubscribedAsset(child record). So that, every subscription has many asset. My problem is, how could I reference the lookup fields of child records on different column? what will be the exact 'value' for the output text?

  • 2
    Hi boy george, welcome to SFSE! Please take a moment to scroll through the tour and read How to Ask. Right now this post does not contain any question, which makes it very difficult to assist you. Can you please edit your post to clarify what you are trying to achieve and where you are stuck? – Adrian Larson Apr 16 '18 at 13:22
  • Your edit is an improvement, but you still do not ask any question nor clearly indicate where you are stuck. It seems like the markup you have already gets a parent value in the column. – Adrian Larson Apr 16 '18 at 13:30
  • You appear to be iterating over a list of SBQQ__Subscription__c, each record within which has detail objects within SBQQ__SubscribedAssets__r, and what you want is to reference a field on one of the detail objects. If that is correct, you need a nested iteration - or possible a LIMIT 1 on your query; it's not clear what you are trying to achieve. – David Reed Apr 16 '18 at 13:32
  • yes I want to reference a lookup field on the detail object. How could I apply the nested iteration there? – boy george Apr 16 '18 at 13:45
2

What's happening here is that each SBQQ__Subscription__c in oldSubscriptionAndAsset has a property SBQQ__SubscribedAssets__r, whose value is a List. What you are trying to reference is a property SBQQ__Product__r.Name on the objects contained in that list.

You cannot do that with oldSubscriptionAndAsset.SBQQ__Product__r.Name, because that is not a property of the type SBQQ__Subscription__c. You also cannot do oldSubscriptionAndAsset.SBQQ__SubscribedAssets__r.SBQQ__Product__r.Name, because oldSubscriptionAndAsset.SBQQ__SubscribedAssets__r is also a List.

To access this property, you must be iterating over the child objects in SBQQ__SubscribedAssets__r. You can do this in two ways, and it's not clear from your question which one is correct.

  1. You can use a nested iteration, where you use an outer loop (probably an <apex:repeat>; you can't use an <apex:pageBlockTable> as an outer loop here) to iterate over the SBQQ__Subscription__c records in oldSubscriptionAndAsset. Then, you can use an inner loop, which could be an <apex:pageBlockTable>, to iterate over the list oldSubscriptionAndAsset.SBQQ__SubscribedAssets__r. Inside the inner iteration, you can access properties like SBQQ__Product__r.Name through its iteration variable.
  2. If your Visualforce page is really only looking at the children of a single SBQQ__Subscription__c record, you can change your query. If you change the type of oldSubscriptionAndAsset from List<SBQQ__Subscription__c> to SBQQ__Subscription__c and query for exactly one record, you no longer need a nested iteration. you would then do

SBQQ__Subscription__c oldSubscriptionAndAsset = [SELECT Id, SBQQ__Product__r.Name, SBQQ__Quantity__c,(SELECT Id, SBQQ__Asset__r.Name, SBQQ__Asset__r.SerialNumber, SBQQ__Asset__r.Is_Selected_For_Renewal__c FROM SBQQ__SubscribedAssets__r) FROM SBQQ__Subscription__c WHERE Id = :SOME_ID LIMIT 1];

The value attribute on your <apex:pageBlockTable> would then be {! oldSubscriptionAndAsset.SBQQ__SubscribedAssets__r }, and your columns could directly reference properties of the child objects.

  • how could I apply on single row the parent record on first column(that will display as single because it's only 1 parent record here), and on second column will be the related child records(that will be multiple) in visualforce page? – boy george Apr 17 '18 at 2:02
  • You still need two iteration components, but one of them would be nested in a column and iterate over SBQQ__SubscribedAssets__r. – David Reed Apr 17 '18 at 2:03
  • can you guide me on that by giving an example? – boy george Apr 17 '18 at 2:04
  • I would recommend reading in detail the references on <apex:repeat> and <apex:pageBlockTable>. I'm afraid I don't have an example handy. – David Reed Apr 17 '18 at 2:12
  • I've already tried that one, but still displaying many parent record. – boy george Apr 17 '18 at 2:13

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.