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
salesVteamssalesVteams 

Invalid foreign key relationship

Hi,

 

I am trying to fetch child object field but its showing error "Invalid foreign key relationship: Book__c.brels__r" to me. Code i am using to fetch the filed is given below:

 

updatebook  = [select a.id, a.Name, a.Price__c,(SELECT b.Bookcolor__c FROM a.brels__r b) FROM Book__c a where id = :ApexPages.currentPage().getParameters().get('id')];//[select id, Name, Price__c from Book__C where id = :ApexPages.currentPage().getParameters().get('id')];
     name = updatebook.Name;
     price = updatebook.Price__c;
     color = updatebook.brels__r.Bookcolor__c;

 

Please help me if some one can do. Waiting for your eply.

 

 

Thanks

SurekaSureka
Hi,

Try using the plural label of the object with __r. For eg. If the object name is Sale, and the plural label is Sales, use Sales__r

Thanks

salesVteamssalesVteams

Hi,

 

Thanks for reply, but i have already used plural one like "color = updatebook.brels__r.Bookcolor__c;"

 

but its not working.

Jeff MayJeff May

Instead of a nested SELECT, how about just returning the related object in the SOQL.  

Vinit_KumarVinit_Kumar

Hi ,

 

Can you make the following change in your SOQL and see if that works :-

 

from 

 

 [select a.id, a.Name, a.Price__c,(SELECT b.Bookcolor__c FROM a.brels__r b) FROM Book__c a where id = :ApexPages.currentPage().getParameters().get('id')];

 

to

 

 [select id, Name, Price__c,(SELECT Bookcolor__c FROM brels__r ) FROM Book__c where id = :ApexPages.currentPage().getParameters().get('id')];