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
Satya413Satya413 

Search method not returning multiple records.

Hi,

I am trying to do a search on custom object Department__c through standard 'Name' field. However, I am unable to retrieve multiple records and only the Name field is being retrieved. Any help is appreciated. 

VF Page:

<apex:page controller="searchdept_con" showHeader="false">
 <apex:form >
  <apex:pageBlock >
   <apex:pageBlockSection Title="Search Department">
      Enter Department Name<apex:inputtext value="{!sd}"/>
      <apex:commandButton value="Find" action="{!searchdept}"/> 
  </apex:pageBlockSection>
  <apex:pageBlockSection title="Department Details" columns="1">  
     <apex:outputField value="{!dept.name}"/>
     <apex:outputField value="{!dept.Phone__c}"/>
     <apex:outputField value="{!dept.Email__c}"/>
     <apex:outputField value="{!dept.Comments__c}"/>
  </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Controller:

public class searchdept_con {

  public string sd { get; set; }
  public department__c dept { get; set; }
  
  public pagereference searchdept()
  {
    string qry = 'select id, name, phone__c, email__c, comments__c from department__c ' + 'where name like \'%'+sd+'%\' ';
    dept = database.query(qry);
    return null;
  }
}

Thank you.
Satya
Best Answer chosen by Satya413
Satya413Satya413
Found the solution. 

VF Page:

<apex:page controller="searchdept_con" showHeader="false">
 <apex:form >
  <apex:pageBlock >
   <apex:pageBlockSection Title="Search Department">
      Enter Department Name<apex:inputtext value="{!sd}"/>
      <apex:commandButton value="Find" action="{!searchdept}" reRender="pb"/> 
   </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
  <apex:pageBlock title="Department Details" id="pb">
   <apex:pageblockTable value="{!deptlist}" var="item">
     <apex:column value="{!item.name}"/>
     <apex:column value="{!item.Phone__c}"/>
     <apex:column value="{!item.Email__c}"/>
     <apex:column value="{!item.Comments__c}"/>
   </apex:pageblockTable>  
  </apex:pageBlock>
</apex:page>

Controller: 

public class searchdept_con {

  public string sd { get; set; }
  public list<department__c> deptlist { get; set; }
  
  public pagereference searchdept()
  {
    string qry = 'select id, name, phone__c, email__c, comments__c from department__c ' + 'where name like \'%'+sd+'%\' ';
    deptlist = database.query(qry);
    return null;
  }
}

Thank you,
Satya