+ Start a Discussion
RICARDO PALMARICARDO PALMA 

Error: Compile Error: Didn't understand relationship

Hello all,

I have a custom relation master/detail  (Qoutes__r) between Quote and QuoteLineItem.
I have a class where i need to get some data from Quote and QuoteLineItem but I'm getting this error:
 
Error: Compile Error: Didn't understand relationship 'Quotes__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 35 column 16

Here my code:

LstQuotes = [SELECT q.id,q.QuoteNumber, q.Ad_Name__c,q.Type__c, q.Size__c,  q.Version__c, q.Bleed_Code__c,
   (Select Quotes__r.Advertiser_Account__c From Quotes__r)
   From Quote q
   where City_Code__c = :theId and q.Media_Name__c =: strMediaID];

The error is on this line (Select Quotes__r.Advertiser_Account__c From Quotes__r)

Thanks.
Best Answer chosen by RICARDO PALMA
AshwaniAshwani
Write query as:

LstQuotes = [SELECT q.id,q.QuoteNumber, q.Ad_Name__c,q.Type__c, q.Size__c,  q.Version__c, q.Bleed_Code__c,
   (Select Advertiser_Account__c From Quotes__r)
   From Quote q
   where City_Code__c = :theId and q.Media_Name__c =: strMediaID];
If it doesn't work check the referenced quote field name. As the error says you are using worng relationship name.

All Answers

AshwaniAshwani
Write query as:

LstQuotes = [SELECT q.id,q.QuoteNumber, q.Ad_Name__c,q.Type__c, q.Size__c,  q.Version__c, q.Bleed_Code__c,
   (Select Advertiser_Account__c From Quotes__r)
   From Quote q
   where City_Code__c = :theId and q.Media_Name__c =: strMediaID];
If it doesn't work check the referenced quote field name. As the error says you are using worng relationship name.

This was selected as the best answer
AshlekhAshlekh
Hi,

Use below code 
LstQuotes = [SELECT id,QuoteNumber, Ad_Name__c,Type__c, Size__c,  Version__c, Bleed_Code__c,Advertiser_Account__c ,(Select id From Quotes__r)
   From Quote where City_Code__c = :theId and Media_Name__c =: strMediaID];
IF it helps you than please mark it as a solution and ENJOY APEX
Vinit_KumarVinit_Kumar
First thing first I do not understand what is the purpose of inner child query coz it seems that you want to access Advertiser_Account__c which is on Quote(parent) object then you should use below query :-

LstQuotes = [SELECT id,QuoteNumber, Ad_Name__c,Type__c, Size__c,  Version__c, Bleed_Code__c,Advertiser_Account__c 
   From Quote where City_Code__c = :theId and Media_Name__c =: strMediaID];

Another situation is when your Advertiser_Account__c is on child object and you are accessing it from Parent object quoe then you should use the below one :-

LstQuotes = [SELECT q.id,q.QuoteNumber, q.Ad_Name__c,q.Type__c, q.Size__c,  q.Version__c, q.Bleed_Code__c,
   (Select Advertiser_Account__c From Quotes__r)
   From Quote q
   where City_Code__c = :theId and q.Media_Name__c =: strMediaID];

If this helps,please mark it as best answer to help others :)


AshwaniAshwani
Ricardo,

I am glad to help. You may not receive any data if filters you applied in "WHERE" doesn't satisfy any criteria. Look for City_Code__c and Media_Name__c matched the value passed in variables "theId: and "strMediaId"
RICARDO PALMARICARDO PALMA
Hi all, 
The initial issue was  wrong relationship name.
I'm not getting an error any more, but I'm not getting any data from QuoteLineItem. I'm sure if the relation is working. 
Thanks.

Deepak Kumar ShyoranDeepak Kumar Shyoran
Please check the Child - relationship name for the Quotes__r Object. It seems that you are using a wrong / incorrect relationship name.
Dipesh KumarDipesh Kumar
For parent-to-child relationships, the parent object has a name for the child relationship that is unique to the parent, the plural of the child object name. For example, Account has child relationships to Assets, Cases, and Contacts among other objects, and has a relationshipName for each, Assets, Cases, and Contacts
check this link for help.....
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_understanding.htm