2

I am trying to do nested query but getting error while saving it. Contact and opprotunity have lookup to account. So I am trying to build a nested soql using this. But getting an error while saving Error :

Error: Compile Error: Didn't understand relationship 'Opportunity' 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 7 column 19

My code :

public class nestedSoql{
public List<Account> accLst = new List<Account>();

public List<Account> accountChildInformation(){
     accLst = [SELECT Name, 
                     (SELECT Name 
                      FROM   Contacts 
                      LIMIT  1),
                     (SELECT Name
                      FROM   Opportunity 
                      LIMIT  1)
               FROM  Account
               WHERE Name != null];


    return accLst ;
}
}

Can anyone help me to fix this issue.

2 Answers 2

2

The relationship name between Account and Opportunity is Opportunities, so you need to use that instead when writing your query.

[SELECT Name, 
    (SELECT Name FROM Contacts LIMIT  1),
    (SELECT Name FROM Opportunities LIMIT  1)
FROM  Account
WHERE Name != null]
1

When you trying to access child records like this you have to use the plural form or correct relationship. Refer to Account object in your WSDL to clearly identify the correct relationship name. For this it should be `Opportunities'. So try below.

accLst = [SELECT Name, 
                     (SELECT Name 
                      FROM   Contacts 
                      LIMIT  1),
                     (SELECT Name
                      FROM   Opportunities
                      LIMIT  1)
               FROM  Account
               WHERE Name != null];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .