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
sfdc startsfdc start 

IN soql........

plz tel methods in soql..?
example on NOTIN on account contcat object
Best Answer chosen by sfdc start
SandhyaSandhya (Salesforce Developers) 
Hi,

NOT IN Keyword in SOQL in Salesforce.
Suppose there is a requirement to fetch all accounts record which doesn't have any contacts,
how will we do it?

We can fetch account with their corresponding contact record in a in line query.
for example
list<Account> listOfAccounts = [SELECT id, Name,
                                                 (SELECT id,name FROM Contacts)
                                                  FROM Accounts];
list<Account> accountListwithoutContact = new list<Account>();

for(Account act :listOfAccounts ){
      if(act .Contacts !=null && act .Contacts.size() !=0){
              accountListwithoutContact.add(act)   ;
      }

}

accountListwithoutContact list will contain all account which doesn't have any contacts.

The second approach would be using NOT IN keyword.
list<Account> listOfAccounts = [SELECT Id, Name
                                                  FROM Account
                                                  WHERE id NOT IN (SELECT AccountId FROM Contact)];
Now this is the final list which contains all accounts which don't have any contact

For all method please refer below link.

https://resources.docs.salesforce.com/sfdc/pdf/salesforce_soql_sosl.pdf


Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.


Thanks and Regards
Sandhya