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
Francisco Garcia 34Francisco Garcia 34 

How to query the Accounts with no Cases?

Hey guys,

I am looking to query the accounts that do not have any case. I have managed to query the accounts with cases in the first query statement of the code:
 
public class controllerClass {
    Public Case myCase {get; set;} 
    public List<Case> cases {get; set;} 
    public List<Account> accounts {get; set;} 
    
    
    Public controllerClass(ApexPages.StandardController stdController){        
        this.myCase = (Case)stdController.getRecord();   
        
        //Query Obtains the Accounts with cases and displays them as a table in firstProject.vfp
        this.cases = [SELECT AccountId, Status, Subject FROM Case ORDER BY AccountId DESC];       
    	
        //This line is supposed to obtain the Accounts with no cases
    	this.accounts = [Select Name FROM Account];
    }	
}

How could I use that result to find out the Accounts with no cases? I dont think it is necessary to provide the visualforce page.

Thanks
 
Best Answer chosen by Francisco Garcia 34
Nayana KNayana K
this.accounts = [Select Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Case)];

This will return accounts with no cases.

All Answers

Nayana KNayana K
this.accounts = [Select Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Case)];

This will return accounts with no cases.
This was selected as the best answer
Francisco Garcia 34Francisco Garcia 34
Thank you so much!