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

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]
| improve this answer | |
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];
| 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.