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
utahsalesforcedeveloperutahsalesforcedeveloper 

SOQL Statement Error

I have Leads related to Accounts (Account__c field on Leads).  I am trying to write a SOQL statement that pulls in all Leads and Contacts associated with specific Accounts.  Here is a general SOQL which I thought should work, but keeps giving me an error message.
acc = [select id,name,industry,billingcountry,createdbyid,(select id,name,email,phone from contacts), (select id, FirstName, LastName from leads) from account limit 20];
I keep getting this error no matter how I try to pull the Lead relationship:  COMPILE ERROR: Didn't understand relationship 'leads' 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.

Any help would be greatly appreciated.
Thank you.
Best Answer chosen by utahsalesforcedeveloper
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
Go into the schema browswer in Eclipse, and go to Account then Child relationships.  Expand the leads and click on the 'Name' field or any other field and see how it builds the query for you.

All Answers

Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

There I no direct relationship exist between account and lead object.When a lead is converted into Account,Contact and opportunity there is some field in lead that contains the id of new Account, Contact and opportunity.

if you want to query the related lead then you have to extra soql query for that.
Refer the below code snippet as reference:

SELECT Id FROM Lead where ConvertedAccountId='0019000000njFTqAAM'
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
Go into the schema browswer in Eclipse, and go to Account then Child relationships.  Expand the leads and click on the 'Name' field or any other field and see how it builds the query for you.
This was selected as the best answer
utahsalesforcedeveloperutahsalesforcedeveloper
Thanks d.phillippi1.388438410478835E12.  You were right on.  Got it working!!