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
Akash DeokarAkash Deokar 

unable to query parent object field from opportunity

I have created one lookup field of custom object(Legal Entity) on opportunity object. Now i want records of opportunity where SAP code field is blank on Legal Entity Object.

I have written query as below but it is not getting executed.

Select id, name, Legal_Entity__r.SAP_CODE__c from opportunity

Error:Didn't understand relationship 'Legal_Entity__r' in field path. 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.
Best Answer chosen by Akash Deokar
Dipika RajputDipika Rajput
Hi Akash, 
Can you look for API name of relationship field name between opportunity and Legal Entity.(I mean to say lookup field API name which is created from Opportunity to Legal Entity)? if your lookup field api name is Legal_Entity__c then your query looks perfectly fine. And f that lookup field API name is something different then use that API name by appending __r while fetching records.

For Example my lookup relationship fileld name from opportunity to Legal Enity is Legal_Entity_rel__c,  then my query will be change as follows:
List<Opportunity> opportunites = [Select id, name, Legal_Entity_rel__r.SAP_CODE__c from opportunity];

 Let me know if it solves your problem and mark this answer as correct answer so that others can get help out of it.

 

All Answers

Dipika RajputDipika Rajput
Hi Akash, 
Can you look for API name of relationship field name between opportunity and Legal Entity.(I mean to say lookup field API name which is created from Opportunity to Legal Entity)? if your lookup field api name is Legal_Entity__c then your query looks perfectly fine. And f that lookup field API name is something different then use that API name by appending __r while fetching records.

For Example my lookup relationship fileld name from opportunity to Legal Enity is Legal_Entity_rel__c,  then my query will be change as follows:
List<Opportunity> opportunites = [Select id, name, Legal_Entity_rel__r.SAP_CODE__c from opportunity];

 Let me know if it solves your problem and mark this answer as correct answer so that others can get help out of it.

 
This was selected as the best answer
Akash DeokarAkash Deokar
Thanks, i was using wrong API name.