+ Start a Discussion
Vikram Singh 157Vikram Singh 157 

I am trying to retrive records from parent to child relationship but it will dispaly error when i am using API name of contact in subquery as "Contact" but when i am using "Contacts" code will run why ?

Account a = [ Select Name,(Select FirstName,LastName from Contact) From Account where id = '0017F000007tWcL'];
Best Answer chosen by Vikram Singh 157

All Answers

Sandeep WaliaSandeep Walia
Hi Vikram,

You are getting this error because while querying the child records you have Query from Child Relationship Name which can be easily found on the workbench.
The correct query will be
Account a = [ Select Name,(Select FirstName,LastName from Contacts) From Account where id = '0017F000007tWcL'];

This 'Contacts' relationshipname was retrieved from workbench as shown in the screenshot below.
 User-added image

Kindly keep this in mind while querying child objects in the future.

Hope this helps,
Sandeep
Shweta_AgarwalShweta_Agarwal
Hi Vikarm,

In Inner query you need to use Child Relationship Name  not the object API name. 
 User-added image

Thanks
Shweta
Sandeep WaliaSandeep Walia
This was selected as the best answer