I have 2 Custom Objects - Package_Item__c, and Product__c. Package_Item__c has two fields - Quantity(Number) and Product(Lookup to Product__c). Is there any way I can access the Package_Item__c.Quantity__c field from a Product__c Object? Here is the code I tired

public List<Product__c> prodlist{get; set;}
prodlist = [SELECT Name,
                    (SELECT Package_Item__c.Quantity__c FROM Product__c.Package_Item__c)
                    FROM Product__c]

Also I have a datatable in VF page, where I have to access the Quantity values. Here's what I tried -

<apex:dataTable value="{!prodlist}" var="p">
           <apex:inputField required="false" value="{!p.Package_Item__r.Quantity__c}"/>

how can I access the quantity values in the inputfield?

1 Answer 1

public Product__c getProduct() {
    return  [SELECT Name, 
      (SELECT Quantity__c FROM Package_Items__r)
        FROM Product__c WHERE Id = :someProductId][0];

and the VF dataTable is:

<apex:datatable value="{!product.Package_Items__r}" var="pi">
  <apex:inputField required="false" value="{!pi.Quantity__c}"/>

Reference: Relationship queries in SOQL

  • This works, but only for one Record. I want to return a List of all the records, and changing the Apex code to a list gives me the runtime error - "Unknown property 'VisualforceArrayList.Package_Items__r' Error is in expression '{!product.Package_Items__r}' in component <apex:dataTable> in page"
    – An Amateur
    Commented Dec 11, 2020 at 5:44
  • if you have multiple products, each with multiple packageItems that you want to display, then your VF page needs to be nested datatables. It is possible I don't understand your requirement and you should edit your OP to provide an example of the intended result. Please don't respond in a comment thread.
    – cropredy
    Commented Dec 11, 2020 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .