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
Sam1980Sam1980 

How to display error message if the datatable is empty(datatable having no results) in component?

Best Answer chosen by Sam1980
James LoghryJames Loghry

In your controller, you can use ApexPages.addMessage() to add a message if your list is empty, and then use apex:pageMessages to display it on your Visualforce page.  https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_pageMessages.htm

Y
our controller might look something like:

public class MyController{
    List<sObject> myRecords {get; private set;}
    public MyController(ApexPages.StandardController ctrl){
        queryRecords();
    }

    public PageReference queryRecords(){
        myRecords = [Select Id From MyObject];

        if(myRecords.isEmpty()){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'List is empty'));
        }
        return;
    }
}

And your VF page may look like:

<apex:page standardController="Account" extensions="MyController">
    <apex:pageMessages />
    <apex:dataTable value="{!myRecords}" />
</apex:page>


All Answers

Denis VakulishinDenis Vakulishin
emm.. I don't understand what you're trying to do. But you can add just, for example <h3 style="color:red"><b>{!errors}</b></h3> to your component and set error message into "error" variable in your component's controller.

Hope it helps.
If you have problems let me know.
James LoghryJames Loghry

In your controller, you can use ApexPages.addMessage() to add a message if your list is empty, and then use apex:pageMessages to display it on your Visualforce page.  https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_pageMessages.htm

Y
our controller might look something like:

public class MyController{
    List<sObject> myRecords {get; private set;}
    public MyController(ApexPages.StandardController ctrl){
        queryRecords();
    }

    public PageReference queryRecords(){
        myRecords = [Select Id From MyObject];

        if(myRecords.isEmpty()){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'List is empty'));
        }
        return;
    }
}

And your VF page may look like:

<apex:page standardController="Account" extensions="MyController">
    <apex:pageMessages />
    <apex:dataTable value="{!myRecords}" />
</apex:page>


This was selected as the best answer
Sam1980Sam1980
Thanks for the reply.Its working now.. :)
Denis VakulishinDenis Vakulishin
Yes, you cant do this like this 
<apex:page standardController="Account" extensions="MyController">
    <apex:outputPanel rendered="{!isErrorVisible}">
      <div style="color:red"><b>{!errorMessage}</b></div>
    </apex:outputPanel>
    <apex:dataTable value="{!myRecords}" />
</apex:page>
Just add is 
isErrorVisible : Booleand
errorMessage : String
to your controller.
You can use ApexPages.getMessages() method to retieve messages in contoller.

Denis VakulishinDenis Vakulishin
<apex:component controller="findMapKey" access="global">
    <div id="introArea" class="grayBg" style="width: 100%;height: 520px;text-align: center;padding-top: 200px;background-image: url('/resource/1404321030000/CMapsDesigner/img/splash.png');background-repeat: no-repeat;background-position-y:0px;background-size: cover;">
        <input type="file" id="fiOpenMap" style="display:none"/>
    
       <apex:attribute name="myValue" description="This is the value for the component."  type="String" />
      <apex:pageMessages />
   
         <apex:dataTable value="{!MapKey}" var="mkey">
        <apex:column >
      
        <button style="width: 200px;height: 50px;"
        onClick="location.href='/resource/1404321030000/CMapsDesigner/designer.html?usessl=true&authkey={!mkey.mapKey__c}'"
        type="button" class="btn btn-default">
        Create a Geographic Map
        </button>
        </apex:column>
      
        </apex:dataTable>
       
             
        <button style="width: 200px;height: 50px;"
        onClick="alert('Coming Soon')"
        type="button" class="btn btn-default">
        Create an Indoor Map
        </button>
   
    <apex:outputPanel id="marketinginfo" redered={hasErrors}>
    Need help? Contact us at support@centigonsolutions.com
    </apex:outputPanel>
   
    </div>
   
  </apex:component>
And controller 
public class findMapKey {
        
    private final List<GMapsPluginEnterpriseTrial__c> obj_mapkey;
     public Integer count {get;set;}
    public findMapKey() {
  
       obj_mapkey= [select mapKey__c from GMapsPluginEnterpriseTrial__c where  expirationDate__c < TODAY and isActive__c = true and Type__c='Designer' limit 1 ];
       count = [select count() from GMapsPluginEnterpriseTrial__c where  expirationDate__c < TODAY and isActive__c = true and Type__c='Designer' limit 1 ];
       
    }
    public Boolen hasErrors{get;set;}
    public List<GMapsPluginEnterpriseTrial__c> getMapKey() {
   
          if(obj_mapkey.isEmpty()){
                hasErrors=true;
            }
        return obj_mapkey;
    }
}

Try this and tell me if this helps or not
usersfdc21usersfdc21
Thank you Denis, It helped me to show different colors for error messages on visulforce page.
.12.12
How can i get this in lightning component