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
cjencjen 

how can i display a message in VF when no results are returned?

hi, i have the follwing class and a VF page that will show the results if records are found. If there are no records for any query, then i see an error:

List has no rows for assignment to SObject
An unexpected error has occurred. Your development organization has been notified.

How can i modify the code to display a message in VF when there are no records returned? this will need to be for each query. Any help is appreciated! thanks!

CLASS:
public class extendAccountsChannel2 { 
    public Investor_Channel__c channels{get; set;} 
    public Investor_Channel__c channelsWholesale{get; set;} 
    public Investor_Channel__c channelsMiniCorrespondent{get; set;} 
    public Investor_Channel__c channelsCorrespondentAOTDirectTrade{get; set;} 
    public Investor_Channel__c channelsCorrespondentBestEfforts{get; set;} 
    public Investor_Channel__c channelsCorrespondentMandatoryBulkFlow{get; set;}
    public Investor_Channel__c channelsCorrespondentNonDelegated{get; set;} 
    public Investor_Channel__c channelsCorrespondentNonDelMandatory{get; set;}   
    
    public Account accounts {get;set;} 
    
    public Account acc {get;set;}
    
    public extendAccountsChannel2(ApexPages.StandardController controller) {
        Id id = ApexPages.currentPage().getParameters().get('id');
        if(ApexPages.currentPage().getParameters().get('id') != null){
            
           id accRecId = [select id from Account where id = :id].id;
            if(accRecId != null){
                channels = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Retail'];
                channelsWholesale = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Wholesale'];  
                channelsMiniCorrespondent = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Mini Correspondent']; 
                channelsCorrespondentAOTDirectTrade = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent AOT / Direct Trade']; 
                channelsCorrespondentBestEfforts = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Best Efforts']; 
                channelsCorrespondentMandatoryBulkFlow = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Mandatory (Bulk/Flow)']; 
                channelsCorrespondentNonDelegated = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Non Delegated'];
                channelsCorrespondentNonDelMandatory = [SELECT Id,name from Investor_Channel__c WHERE Account_Name__r.id= :accRecId AND Channel__c = 'Correspondent Non-Del Mandatory'];
                
                 
            }
        }
        
        
        
    }
    
}




VF:
 
<apex:page standardController="Account" extensions="extendAccountsChannel2"> <style> .activeTab {background-color: #003366; color:white; background-image:none;} .inactiveTab { background-color: lightgrey; color:black; background-image:none;} input.btn[name="del"] { display: none; } input.btn[name="clone"] { display: none; } input.btn[name="edit"] { display: none; } </style> 

<apex:tabPanel switchType="ajax" id="theTabPanel" tabClass="activeTab" inactiveTabClass="inactiveTab" > <apex:tab label="Correspondent AOT / Direct Trade" name="name4" id="tabFour" style="background-color: white;" > <apex:detail subject="{!channelsCorrespondentAOTDirectTrade}" relatedList="false" /> </apex:tab> <apex:tab label="Correspondent Best Efforts" name="name5" id="tabFive" style="background-color: white;" > <apex:detail subject="{!channelsCorrespondentBestEfforts}" relatedList="false" /> </apex:tab> <apex:tab label="Correspondent Mandatory (Bulk/Flow)" name="name6" id="tabSix" style="background-color: white;" > <apex:detail subject="{!channelsCorrespondentMandatoryBulkFlow}" relatedList="false" /> </apex:tab> <apex:tab label="Correspondent Non Delegated" name="name7" id="tabSeven" style="background-color: white;" > <apex:detail subject="{!channelsCorrespondentNonDelegated}" relatedList="false" /> </apex:tab> <apex:tab label="Correspondent Non-Del Mandatory" name="name8" id="tabEight" style="background-color: white;" > <apex:detail subject="{!channelsCorrespondentNonDelMandatory}" relatedList="false" /> </apex:tab> <apex:tab label="Mini Correspondent" name="name3" id="tabThree" style="background-color: white;" > <apex:detail subject="{!channelsMiniCorrespondent}" relatedList="false" /> </apex:tab> <apex:tab label="Retail" name="name1" id="tabOne" style="background-color: white;" > <apex:detail subject="{!channels}" relatedList="false" /> </apex:tab> <apex:tab label="Wholesale" name="name2" id="tabTwo" style="background-color: white;" > <apex:detail subject="{!channelsWholesale}" relatedList="false" /> </apex:tab> </apex:tabPanel> <br/> </apex:page>

 
GauravGargGauravGarg
Hi Cjen,

First of all, always try to use FOR SOQL i.e. 
for(Account acc : [Query])


For SOQL is very powerful as this will handle all the exception occurred while processing including "No Record Found" Or Null Pointer. 

Secondly, before returning the variable on the VF side, check if there are records else, return "No Record Found." message on the VF page to display it using re-rendering. 

Hope this helps. 

Thanks,

Gaurav
Skype: gaurav62990
email: gauravgarg.nmims@gmail.com

Suraj MakandarSuraj Makandar
Hi Cjen,

When we store SOQL output as sObject it is  mandatory that the SOQL returns atleast one record else you will get below error:
List has no rows for assignment to SObject

To resolve this issue always store your SOQL results in List, for ex:

All your querires are in below format:

Account acc = [Select Id from Account Limit 1];

Change your queries to store result as List, like:

List<Account> acc = [Select Id from Account Limit 1];

if(!acc.isEmpty()){
    //do your processing here.
}else{
  //display message when no records found
}


Thanks,
Suraj
cjencjen
hi, and thanks for reposnding. i am confused because it's the Investor Channel object that could return 0 records, not the account. The visualforce page is embedded on the account and i want to find the related Investor Channel records (there should only be one for each channel picklist value). The requirement is to see the detail page of each Investor channel record on the account page. They don't want to use a related list. The code i provided works fine as long as there is 1 Investor Channel record per query. but gives the error if even one query does not return a record. i would like a message displyed in the visualforce apex:tab for each query that does not have a record, and still display the detail page of the other found records. Any help with that?
GauravGargGauravGarg
Hi Cjen,

If you look into my previous comment, there is a way to avoid "0" record error via for soql instead of a normal query. 

Here is the document (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm)to verify it.

Still have doubts, connect via Skype: gaurav62990, email: gauravgarg.nmims@gmail.com

Thanks,
Gaurav