I've a parent object (Product) and a child (Inventory). I'm trying to retrieve the value of the child object from a class that I've created.

public class DisplayProducts {
    private Product__c products;

public DisplayProducts(Product__c item) {
    this.products = item;


// Properties for use in the Visualforce view
public String name {
    get { return products.Name; }

public String colour {
//error here 
        get { return  products.Inventorys__r.Colour__c; }
//products.Inventorys__r[0].Colour__c; --> only retrive the first element, dosen't seems to work

public class Product {

public List<DisplayProducts> getProducts() {

        if(products == null) {
            products = new List<DisplayProducts>();

            for(Product__c item : [Select ProductID__c,Name, Price__c, (SELECT Inventory__c.Size__c, Inventory__c.Colour__c,  Inventory__c.Quantity__c FROM Product__c.Inventorys__r) 
                From Product__c WHERE ProductID__c = :prodID]) {

                products.add(new DisplayProducts(item));

    return products;


However I keep getting a compile error: invalid FK relationship. Why is that so?

When I retrieve the object using SOQL statement it works perfectly.

Select Name,  (SELECT Inventory__c.Colour__c FROM Product__c.Inventorys__r)  From Product__c];

but why it doesn't work in the Display Class? Thank you.

  • You can't "go through relation" with . (dot) in Apex. Have you tried building a query in the code to select the data needed from the Inventorys object based on the Id of the product Dec 25, 2014 at 13:24
  • @MihaiNeagoe Hi, I've tried building a query based on the SOQL found on mu question. But how do I retrieve through the class that Ive created above?
    – taradyn
    Dec 25, 2014 at 17:01

1 Answer 1


products.Inventorys__r will return list of child records .

The return type of your method is a string .Hence you may need to return a list of child records .

If you need the child array you can use something like below

  public List< Inventory__c> colour {
        get { return  products.Inventorys__r;}//return list of Inventory records and this would be list data type
  • thanks for you reply. I've tried your method. However I got an error message List index out of bounds: 0 .
    – taradyn
    Dec 25, 2014 at 16:20
  • You have no child data hence the issue.Add an if clause to check size of the list. Dec 25, 2014 at 17:11
  • sorry for the silly question. Got it!
    – taradyn
    Dec 25, 2014 at 17:26
  • Can i check with you, currently my list has multiple items. How do i loop through products.ProductInventorys__r.Colour__c;? Coz your code only retrieve the first element from the item. Thanks!
    – taradyn
    Dec 25, 2014 at 17:45
  • For(Inventory__c i:products.inventorys){ system.debug(i); } Dec 25, 2014 at 17:57

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.