function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
taradyntaradyn 

Invalid FK relationship in VF


0down votefavorite

I've a parent object (Product) and a child (Inventory). I'm trying to retrieve the value of the child object from a DisplayProducts 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; }

    }

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;
}

}



I keep getting a compile error: invalid FK relationship.

I tried but it will only retrieved the first element.
products.Inventorys__r[0].Colour__c;

How do I retrieve the child item via the DisplayProducts Class? Any help will be greatly appreciated.

Thank you.
Best Answer chosen by taradyn
Anoop yadavAnoop yadav
Hi,
Try with the below code.
public List<String> colour {
	List<Inventory__c> inventories = new List<Inventory__c> ();
	List<String> colours = new List<String>();
	
	inventories = [Select Id, Colour__c From Inventory__c WHERE Product__c = :products.Id];
	for(Inventory__c invt :inventories){
		colours.add(invt.Colour__c);
	}
    return colours[0];	
}

 

All Answers

Anoop yadavAnoop yadav
Hi,

Replace the below method
public String colour {
//error here 
        get { return  products.Inventorys__r.Colour__c; }

    }
with the below one
public List<String> colour {
	List<Inventory__c> inventories = new List<Inventory__c> ();
	List<String> colours = new List<String>();
	
	inventories = [Select Id, Colour__c From Inventory__c WHERE Product__c = :products.Id];
	for(Inventory__c invt :inventories){
		colours.add(invt.Colour__c);
	}
	return colours;
}

Product__c is the lookup field on child.

It will return you all the chield records.

 
taradyntaradyn
Hi @Anoop, thanks for your reply. I would like to return only 1 colour item from the List<String>. Is there a way for me to do that?
Anoop yadavAnoop yadav
Hi,
Try with the below code.
public List<String> colour {
	List<Inventory__c> inventories = new List<Inventory__c> ();
	List<String> colours = new List<String>();
	
	inventories = [Select Id, Colour__c From Inventory__c WHERE Product__c = :products.Id];
	for(Inventory__c invt :inventories){
		colours.add(invt.Colour__c);
	}
    return colours[0];	
}

 
This was selected as the best answer