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
Linda 98Linda 98 

Display SOME data when page loads...Please help

Hi .

I would like to display some conditional data when my VF page loads.I had written my soql query in constructor.But i am not sure how to dispaly in VF page.
My Vf page is a search page.So when page loads i want that data to show up .When search is done,that data has to disappear and my search results has to be showed up.

Please help.

Here is my VF page and apex class

<apex:page standardController="customobject__c" extensions="searchpageController">
<apex:form >
<apex:outputPanel id="errorPanel">
        <apex:pageMessages />
    </apex:outputPanel>

   <apex:pageblock title="Searach Records" >
  
       <apex:outputText value="Name"/>&nbsp;&nbsp;&nbsp;
       <apex:inputtext value="{!inputNametext}"/> &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      
       <apex:outputText value="Account Name"/>
       <apex:inputtext value="{!inputAccounttext}"/> &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      
       <apex:outputtext value="Contact Name"/>
       <apex:inputtext value="{!inputContacttext}"/> &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      
       <apex:commandbutton value=" Search " action="{!searchRecords}"/>
   </apex:pageblock>
   <apex:pageBlock >
     <apex:pageblocktable value="{!accList}" var="acc">
      <apex:column >
         <apex:outputLink value="/{!acc.id}"><apex:outputText value="{!acc.Name}" />
         </apex:outputLink>
         </apex:column>
      <apex:column value="{!acc.Account__c}"/>
      <apex:column value="{!acc.Contact__c}"/>
     </apex:pageblocktable>    
     </apex:pageBlock>
</apex:form>
</apex:page>

Apex class:

Public class  FARsearchpageController {
   Public string inputNametext{get;set;}
   Public string inputAccounttext{get;set;}
   Public string inputContacttext{get;set;}
   Public string inputstatustext{get;set;}
   Public String soql{get;set;}
   Public List<Fuel_Analysis_Report__c> accList{get;set;}
   Public String usernames=UserInfo.getName();

   public FARsearchpageController (ApexPages.StandardController controller) {
       String soql = 'select Name,Account__c,ContactStatus__c,Closed_Date__c  from customobject__c  where name != null and Name =:Usernames';
    }
       
   Public void searchRecords(){
     soql = 'select Name,Account,Contact,Status__c,Closed_Date__c from customobject__c where Name =:Usernames';

if (string.isnotBlank(inputAccounttext)){
    soql += ' and Account.Name like '+'\''+'%'+inputaccounttext+'%'+'\'';
    }
    if (string.isnotBlank(inputnametext)){
    soql += ' and Name like '+'\''+'%'+inputnametext+'%'+'\'';
       }
    if (string.isnotBlank(inputContacttext)){
    soql += ' and Contact.Name like '+'\''+'%'+inputcontacttext+'%'+'\'';
       }
    accList= Database.query(soql);
    }
  }
}
}
CheyneCheyne
You need to execute the query in your constructor to get the data to show up when the page loads initially. You need to add a reRender attribute to the search command button to get the new list to display when that button is clicked. Also, the extension name in your visualforce page does not seem to match the actual controller name, and the soql queries are not querying the same object as your list definition. Additionally, when you are using dynamic soql, as you are here, you cannot bind variables the way you are doing it. You have to concatenate the strings, and make sure that they are enclosed in single quotes. Here's your code, with the additions in bold:

<apex:page standardController="customobject__c" extensions="FARsearchpageController">
<apex:form >
<apex:outputPanel id="errorPanel">
        <apex:pageMessages />
    </apex:outputPanel>

   <apex:pageblock title="Searach Records" >
 
       <apex:outputText value="Name"/>&nbsp;&nbsp;&nbsp;
       <apex:inputtext value="{!inputNametext}"/> &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
     
       <apex:outputText value="Account Name"/>
       <apex:inputtext value="{!inputAccounttext}"/> &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
     
       <apex:outputtext value="Contact Name"/>
       <apex:inputtext value="{!inputContacttext}"/> &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
     
       <apex:commandbutton value=" Search " action="{!searchRecords}" reRender="accListBlock" />
   </apex:pageblock>
   <apex:pageBlock id="accListBlock">
     <apex:pageblocktable value="{!accList}" var="acc">
      <apex:column >
         <apex:outputLink value="/{!acc.id}"><apex:outputText value="{!acc.Name}" />
         </apex:outputLink>
         </apex:column>
      <apex:column value="{!acc.Account__c}"/>
      <apex:column value="{!acc.Contact__c}"/>
     </apex:pageblocktable>   
     </apex:pageBlock>
</apex:form>
</apex:page>

Apex class:

Public class  FARsearchpageController {
   Public string inputNametext{get;set;}
   Public string inputAccounttext{get;set;}
   Public string inputContacttext{get;set;}
   Public string inputstatustext{get;set;}
   Public String soql{get;set;}
   Public List<Fuel_Analysis_Report__c> accList{get;set;}
   Public String usernames=UserInfo.getName();

   public FARsearchpageController (ApexPages.StandardController controller) {
       String soql = 'select Name,Account__c,ContactStatus__c,Closed_Date__c  from Fuel_Analysis_Report__c where name != null and Name=\''+Usernames+'\'';
       accList = Database.query(soql);
    }
      
   Public void searchRecords(){
     soql = 'select Name,Account,Contact,Status__c,Closed_Date__c from Fuel_Analysis_Report__c where Name =\''+Usernames+'\'';

if (string.isnotBlank(inputAccounttext)){
    soql += ' and Account.Name like '+'\''+'%'+inputaccounttext+'%'+'\'';
    }
    if (string.isnotBlank(inputnametext)){
    soql += ' and Name like '+'\''+'%'+inputnametext+'%'+'\'';
       }
    if (string.isnotBlank(inputContacttext)){
    soql += ' and Contact.Name like '+'\''+'%'+inputcontacttext+'%'+'\'';
       }
    accList= Database.query(soql);
    }
  }
}
}
Linda 98Linda 98
Thank you ..It helped.Corrected my typo errors.
Also i would like to display an message when acclist is empty just to say no matching records are found on pageblock table.
My test class also doesnt seem to have code coverage.Plase help.

Thanks in advance!!!
CheyneCheyne
You can add a new element which is rendered when the list size is 0. Use the "rendered" attribute to conditionally display an element. 

<apex:pageBlock id="accListBlock">
    <apex:outputPanel rendered="{!accList.size == 0}">
        No matching records were found.
    </apex:outputPanel>

     <apex:pageblocktable value="{!accList}" var="acc" rendered="{!accList.size > 0}">
      <apex:column >
         <apex:outputLink value="/{!acc.id}"><apex:outputText value="{!acc.Name}" />
         </apex:outputLink>
         </apex:column>
      <apex:column value="{!acc.Account__c}"/>
      <apex:column value="{!acc.Contact__c}"/>
     </apex:pageblocktable>  
     </apex:pageBlock>