Monday, 2 December 2013

NOT IN Keyword in Soql in Salesforce.

Suppose there is a requirement to fetch all accounts record which don'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 dont have any contacts.

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.

4 comments:

Obat Tradisional Kanker Indung Telur Paling Bagus said...

This website is excellent, very great as well as packed with perfect content.
Obat Tradisional Tbc Kelenjar Yang Praktis dan Aman

Unknown said...

Excellent website. Lots of useful information here, thanks for your effort!. For more information please visit

SalesForce online training

kamona said...

Thank you for posting this topic contains a lot of information
------------
شركة تنظيف كنب بمكة

شركة فارس الخليج said...

نحن افضل شركة تنظيف بالمملكة العربية السعودية وهذه شهادة من جميع عملائنا الاعزاء فتواصلوا معنا فشركة الحورس تعمل دائما من اجل تلبية كافة المتطلبات و الاحتياجات
افضل شركة تنظيف بالباحه