SOQL query to get accounts which have atleast one contact in salesforce - Salesforce Globe For You
Solution: Use the query below
Select Id, Name From Account Where Id IN (Select AccountId From Contact)
Example:
Run the following piece of code in the execute anonymous window
List<Account> lstAccountWithContacts = new List<Account>();
lstAccountWithContacts = [Select Id, Name From Account Where Id IN (Select AccountId From Contact)];
system.debug('Number of Accounts with Contacts: '+lstAccountWithContacts.size());
Output:
Note: The above query may give an error if the result has morethan 50000 rows.Use Limit as shown below
Select Id, Name From Account Where Id IN (Select AccountId From Contact) Limit 50000
No comments:
Post a Comment