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
HartleyHartley 

SOQL query to get custom field in standard object from Custom Object

Hey,

I have a Custom object (custom_object__c) with a Lookup field for (Contact) from which I would like to get a field or custom field from a standard object (ie. Contact). If I understood correctly I then need to do a Child - Parent relationship which is in the form of dot notation.

So here is my code:

public static List<custom_object__c> getStudents(){
        LIST<custom_object__c> students = [SELECT 
                Id, 
                Name,
                Contact__r.custom_field__c,
                 custom_object__c__custom_field_1,
                custom_object__c__custom_field_1
                FROM custom_object__c
                LIMIT 100
                OFFSET 0];
        
        return students;
    }

And here is the error message:

Didn't understand relationship 'Contact__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.

Any explanation is greatly appreciated, thank you.
 
Best Answer chosen by Hartley
sfdcMonkey.comsfdcMonkey.com
hi Hartley 
i got it, try this query for get student(contact lookup) fields , campus(account lookup) fields from StudentTermRegistration__c
Select 
  Name,
  Student__r.FirstName,
  Student__r.LastName, 
  Student__r.Id, 
  Student__c,  
  Campus__r.Name, 
  Campus__r.Id, 
  Campus__c 
 From StudentTermRegistration__c

let me inform please if it work and mark this question as solve ;)

All Answers

sfdcMonkey.comsfdcMonkey.com
hi,Hartley 
you can query contact lookup  fields from custome_object__c  by this query 
 
[ Select Id,
       Name,
        Contact__r.FirstName,
        Contact__r.LastName, 
        Contact__c 
       From custom_object__c LIMIT 100 ]
 

If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer. ;-)
HartleyHartley
Hey Soni,

Thanks for the quick responses.

So I'm assuming the part I was missing was the Contact__c within my SELECT statement, however I've updated my query with your feedback and I'm still getting the same error. 

It's still unable to understand relationship Contact__r . Is there maybe a setting or configuration I must make to the Object in order for this to work ? Or am I misunderstanding the relationship between the two Objects ? 

Thanks again.
sfdcMonkey.comsfdcMonkey.com
Can you send your custom object and its fields API names , i check and let you inform  :)
HartleyHartley
Custom Object
--------------------
 
API Name = StudentTermRegistration__c

Fields
--------
Label = Student
API name = Student__c
Type = Lookup(Contact)

Label = Campus
API name = Campus__c
Type = Lookup(Account)

Label = Program Title
API name = Program_Title__c
Type = Formula (Text)

There are other fields but you get the point. This also ties into another question of doing multiple custom relationships on different Objects. Thanks a lot again ! Let me know if you require any further information.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
This should work
[SELECT Id, Name,Student__r.custom_field__c FROM StudentTermRegistration__c LIMIT 100 OFFSET 0];
HartleyHartley
Just to clarify I made a mistake in my post for the fields. It should say Field Name and not API name

Fields
--------
Label = Student
Name = Student__c
Type = Lookup(Contact)

Label = Campus
Name = Campus__c
Type = Lookup(Account)

Label = Program Title
Name = Program_Title__c
Type = Formula (Text)

Which is why the previous provided answer will not work since the Object is StudentTermRegistration__c and the fields for that object are what is listed above. 

Also to be clear what I want to do is get certain fields from the Contact Object from my Custom child object StudentTermRegistration__c and eventually get certain data from the Account Oject from the same custom object. The fields listed above demonstrate the references or Lookups of the Objects from my custom object.
sfdcMonkey.comsfdcMonkey.com
hi Hartley 
i got it, try this query for get student(contact lookup) fields , campus(account lookup) fields from StudentTermRegistration__c
Select 
  Name,
  Student__r.FirstName,
  Student__r.LastName, 
  Student__r.Id, 
  Student__c,  
  Campus__r.Name, 
  Campus__r.Id, 
  Campus__c 
 From StudentTermRegistration__c

let me inform please if it work and mark this question as solve ;)
This was selected as the best answer
HartleyHartley
Hey Soni,

Thanks a lot, that did the trick !!
manibharathi vmanibharathi v
Display how many custom field available in standard
object how i
did