Play Games

Search This Blog

Tuesday, May 24, 2022

SOQL query to get accounts which doesn't have any contacts associated in salesforce - Salesforce Globe For You

SOQL query to get accounts which doesn't have any contacts associated in salesforce -  Salesforce Globe For You

Solution: Use the query below

Select Id, Name From Account Where Id NOT IN (Select AccountId From Contact)

Example:

Run the following piece of code in the execute anonymous window

List<Account> lstAccountWithOutContacts = new List<Account>();

lstAccountWithOutContacts = [Select Id, Name From Account Where Id NOT IN (Select AccountId From Contact)];

system.debug('Number of Accounts without Contacts: '+lstAccountWithOutContacts.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 NOT IN (Select AccountId From Contact) Limit 50000

No comments:

Post a Comment