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
Syed F Raza 10Syed F Raza 10 

IF ELSE Condition in List

Hi All,

I am having trouble with the below code:
 
Public Class SOQLTry{
    
    public static void showAccountsBasedOnRating(String AccountRating){
        
        List<Account> List2 = [SELECT Name, Rating
                               FROM Account
                               WHERE Rating IN (:AccountRating)];
            
        For(Account AAR : List2) {
            system.debug('Name of Account with Rating ' +AAR.Rating+' is '+AAR.Name );
        }
    }
    
    
    public static void showIndustryOfAccounts(String AccountIndustry){
        List<Account> List3 = [SELECT Name, Industry
                               FROM Account
                               WHERE Industry = :AccountIndustry];
        for(Account AAI : List3)
        	{
            IF(List3.isEmpty()==False)
            {
            system.debug(AAI.Name+' belongs to the '+AAI.Industry+' Industry');
            }
            
            ELSE 
            {
                system.debug('There is no account related to ');
            }
        }
    }
}

The showAccountsBasedOnRating() method is executing as per expectations.

The showIndustryOfAccounts() method is executing with upto "IF" command but the ELSE commoand is not executing.

Any idea what could be causing this.

Thanks.
 
Sachin HoodaSachin Hooda
There is no point on checking whether the list is empty or not. Since if the list is empty actually you would never be in the foreach. So, instead you can have something like,
for(Account AAI : List3)
        	{
            if(!AAI.Name.isBlank(){
                 System.debug('Name :'+AAI.Name+' Industry:'+AAI.Industry);
           }
           else{
                   System.debug('account is not related');
           }
Syed F Raza 10Syed F Raza 10
Sachin,

Thanks for your response.
Maharajan CMaharajan C
Hi Syed,

You have to write like this:
 
Public Class SOQLTry{
    
    public static void showAccountsBasedOnRating(String AccountRating){
        
        List<Account> List2 = [SELECT Name, Rating
                               FROM Account
                               WHERE Rating IN (:AccountRating)];
            
        For(Account AAR : List2) {
            system.debug('Name of Account with Rating ' +AAR.Rating+' is '+AAR.Name );
        }
    }
    
    
    public static void showIndustryOfAccounts(String AccountIndustry){
        List<Account> List3 = [SELECT Name, Industry
                               FROM Account
                               WHERE Industry = :AccountIndustry];
		if(!List3.isEmpty())
		{
			for(Account AAI : List3)
			{
				system.debug(AAI.Name+' belongs to the '+AAI.Industry+' Industry');
			}
		}
		else
			system.debug('There is no account related to ' +  AccountIndustry + ' Industry');
    }
}



Thanks,
Maharajan.C