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
MellowYellowMellowYellow 

How to display a message if no records are returned from a SOQL query?

I have a simple VF page that calls a custom controller to retrieve contacts for the account being viewed.  I would like to display a message such as "No records found" if the query returns no records.    I don't know if this should be added to the VF page or the controller.  Code is included below;

 

 


<apex:page controller="MSContactsCon3" showheader="false">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!contacts}" var="o">
           <apex:column headervalue="Name" >
           <apex:outputlink value="/{!o.id}">{!o.name}</apex:outputLink>
           </apex:column>
           <apex:column value="{!o.title}"/>
           <apex:column value="{!o.phone}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


public class MSContactsCon3 {

String accountVar = ApexPages.currentPage().getParameters().get('id');
    public ApexPages.StandardSetController setCon {
     
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT  Id, Title, Phone, Name,  Contact_type__c  FROM Contact Where AccountId =  :AccountVar     AND contact_type__c INCLUDES ( 'Executive Contact') Order by lastname]));
            }
            return setCon;
        }
        set;
    }
    public List<Contact> getContacts()
 {  
         return (List<Contact>) setCon.getRecords();
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
Shebin-KVP Business SolnsShebin-KVP Business Solns

Hi MellowYellow

 

 Please try your code which I have modified.Please feel free for  further clarification on the code

 

 

<apex:page controller="MSContactsCon3" showheader="false">
    <apex:pageBlock >
        <apex:messages  />
        <apex:pageBlockTable value="{!contacts}" var="o">
           <apex:column headervalue="Name" >
           <apex:outputlink value="/{!o.id}">{!o.name}</apex:outputLink>
           </apex:column>
           <apex:column value="{!o.title}"/>
           <apex:column value="{!o.phone}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>



public class MSContactsCon3 {

String accountVar = ApexPages.currentPage().getParameters().get('id');
    public ApexPages.StandardSetController setCon {
     
        get {
            if(setCon == null) {
                  Contact con = new  Contact ();
                  
                  setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT  Id, Title, Phone, Name,  Contact_type__c  FROM Contact Where AccountId =  :AccountVar     AND contact_type__c INCLUDES ( 'Executive Contact') Order by lastname]));
    
                  if(setCon.getResultSize()==0)
                  {
                   ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No records found');
                   ApexPages.addMessage(myMsg); 
                  }
            }
            return setCon;
        }
        set;
    }
    public List<Contact> getContacts()
    {  
         return (List<Contact>) setCon.getRecords();
    }
}

 

All Answers

Shebin-KVP Business SolnsShebin-KVP Business Solns

Hi MellowYellow

 

 Please try your code which I have modified.Please feel free for  further clarification on the code

 

 

<apex:page controller="MSContactsCon3" showheader="false">
    <apex:pageBlock >
        <apex:messages  />
        <apex:pageBlockTable value="{!contacts}" var="o">
           <apex:column headervalue="Name" >
           <apex:outputlink value="/{!o.id}">{!o.name}</apex:outputLink>
           </apex:column>
           <apex:column value="{!o.title}"/>
           <apex:column value="{!o.phone}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>



public class MSContactsCon3 {

String accountVar = ApexPages.currentPage().getParameters().get('id');
    public ApexPages.StandardSetController setCon {
     
        get {
            if(setCon == null) {
                  Contact con = new  Contact ();
                  
                  setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT  Id, Title, Phone, Name,  Contact_type__c  FROM Contact Where AccountId =  :AccountVar     AND contact_type__c INCLUDES ( 'Executive Contact') Order by lastname]));
    
                  if(setCon.getResultSize()==0)
                  {
                   ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No records found');
                   ApexPages.addMessage(myMsg); 
                  }
            }
            return setCon;
        }
        set;
    }
    public List<Contact> getContacts()
    {  
         return (List<Contact>) setCon.getRecords();
    }
}

 

This was selected as the best answer
MellowYellowMellowYellow

Thanks! 

Shebin-KVP Business SolnsShebin-KVP Business Solns

Please set as solved