I have this request: for loop that retrieve 10 object from Acccount and their childs and for each account display the Account and its related childrens

Here is my sample code:

for (Account acc : [SELECT Name, (SELECT LastName,id,Email from Contact) from Account limit : 10]){

Am having the following errors:

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

Any help please

| improve this question | | | | |

The problem is in "FROM Contact"

You can't subselect on an object,but on a relationship to an other object. So you have to use the name of the relationship, which is Contacts in this case

SELECT Name, (SELECT LastName,id,Email from Contacts) from Account limit  10

Note that limit also doesn't require an : or = if you have the value in the query directly. The : is when you are binding to a variable.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.