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
phani_mphani_m 

Dynamic search

Hai............

           Iam new to salesforce i got a problem in coding....

    I had the Candidate (custom object ) with fields first name, last name, city , education,  ineed to get the  record values from that  object but for me no values are displaying .............. can any one help in code and suggest me . here is my controller and visualforce code...........

 

 

 

public with sharing class CandidateSearchController {   
    
    private String soql {get;set;}
    // the collection of contacts to display
      Public List<Candidate__c> candidates {get;set;}
    // the current sort direction. deaults to asc
      public String sortDir{
      get { if (sortDir == null) { sortDir = 'asc'; } 
             return sortDir;
             }
             set;
             }
    // the current field to sort by. defaults to last name 
        public String sortField {
          get {
                if  ( sortField == null) {
                sortField = 'Last_Name__c'; } return sortField; }
                set;
                }
      // format the soql for display on the visualforce page 
           public String debugSoql {
              get { return soql + 'order by ' + sortField + ' ' + sortDir + 'limit 20 '; }
               set;
               }
      // init the contrller and display some sample data when the page loads
          public CandidatesearchController() {
            soql='select First_name__c,Last_name__c,City__c,Education__c from Candidate__c where Candidate__c.First_name__c != null';    
                   runQuery();
                   }
        // toggles the sorting  of query from asc<-->desc
            public void toggleSort() {
              //simply toggle the direction  
                   sortDir = sortDir.equals('asc') ? 'desc' : 'asc' ;
                  // run the query again
                    runQuery();
                    }             
        //runs the actualquery
           public void runQuery() {
              try {
                   candidates = Database.query(soql + 'order by ' + sortField + ' ' + sortDir + ' limit 20 ');
                    }
                    catch (Exception e) {
                     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'OOOps!') );
                     }
                   }                                
    // runs the search with parameters passed via JavaScript
      public PageReference runSearch(){
      
      
          String FirstName = Apexpages.currentPage().getParameters().get('First_Name__c');
    String LastName = Apexpages.currentPage().getParameters().get('Last_Name__c');
    String City = Apexpages.currentPage().getParameters().get('City__c');
    String Education = Apexpages.currentPage().getParameters().get('Education__c');
 
    soql='select First_name__c,Last_name__c,City__c,Education__c from Candidate__c where Candiadate__c.First_Name__c != null';    

    if (!FirstName.equals(''))
      soql += ' and First_Name__c LIKE \' '+String.escapeSingleQuotes(FirstName)+'%\'';
    if (!LastName.equals(''))
      soql += ' and Last_Name__c LIKE \''+String.escapeSingleQuotes(LastName)+'%\'';
    if (!City.equals(''))
      soql += ' and City__c LIKE \''+String.escapeSingleQuotes(city)+'%\'';  
    if (!Education.equals(''))
      soql += ' and Education__c LIKE (\''+Education+'\')';
 
    // run the query again
    runQuery();
 
    return null;
  }
 
  // use apex describe to build the picklist values
  public List<String> Education {
    get {
      
      
      
      if (Education == null) {
 
        Education = new List<String>();
        Schema.DescribeFieldResult field = Candidate__c.Education__c.getDescribe();
 
        for (Schema.PicklistEntry f : field.getPicklistValues())
         education.add(f.getLabel());
 
      }
      return education;          
    }
    set;
  }
 
}

 

 

apex:page controller="CandidateSearchController" sidebar="false">
 
  <apex:form >
  <apex:pageMessages id="errors" />
 
  <apex:pageBlock title="Find Me A Candidate!" mode="edit">
 
  <table width="100%" border="0">
  <tr>  
    <td width="200" valign="top">
 
      <apex:pageBlock title="Parameters" mode="edit" id="criteria">
 
      <script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById("First_Name__c").value,
          document.getElementById("Last_Name__c").value,
          document.getElementById("City__c").value,
          document.getElementById("Education__c").options[document.getElementById("Education__c").selectedIndex].value
          );
      }
      </script> 
 
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="First_Name__c" value="" />
          <apex:param name="Last_Name__c" value="" />
          <apex:param name="City__c" value="" />
          <apex:param name="Education__c" value="" />
      </apex:actionFunction>
 
      <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">First Name<br/>
        <input type="text" id="First_Name__c" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Last Name<br/>
        <input type="text" id="Last_Name__c" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">City<br/>
        <input type="text" id="City__c" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Education<br/>
          <select id="Education__c" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!Education}" var="edc">
              <option value="{!edc}">{!edc}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>
      </table>
 
      </apex:pageBlock>
 
    </td>
    <td valign="top">
 
    <apex:pageBlock mode="edit" id="results">
 
        <apex:pageBlockTable value="{!candidates}" var="candidate">
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="First Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="first_name__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!Candidate.First_Name__c}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Last Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="last_Name__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!candidate.Last_Name__c}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="City" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="City__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!candidate.City__c}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Education" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Education__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!candidate.Education__c}"/>
            </apex:column>
 
        </apex:pageBlockTable> 
 
    </apex:pageBlock> 
 
    </td>
  </tr>
  </table>
 
  <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}" />           
  </apex:pageBlock>    
 
  </apex:pageBlock>
 
  </apex:form>
 
</apex:page>

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Devendra@SFDCDevendra@SFDC

Hi,

 

get { return soql + 'order by ' + sortField + ' ' + sortDir + 'limit 20 '; } order by should be preceded with white space.

 

get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }

 

If you dont provide any value to textfields then null would get considered inside a query. Therefore check for null inside a controller to avoid null exceptions.

 

Thanks,

Devendra

All Answers

Devendra@SFDCDevendra@SFDC

Hi,

 

Is there any specifc error you are getting for above code?

 

Thanks,

Devendra

phani_mphani_m

haii..............

 

 

 no error is displaying ......

 

     on the top of my page ERROR Ooops! message is dispalying automatically if didn't enterd values in text fields

what i observed is under 

DEBUG Sql section----->

select First_name__c,Last_name__c,City__c,Education__c from Candidate__c where Candiadate__c.First_Name__c != null and First_Name__c LIKE ' siva %' and Last_Name__c LIKE 'kumar%' and City__c LIKE 'vijayawada%' and Education__c LIKE ('MBA')order by Last_Name__c asclimit 20 ----- is coming

 

Devendra@SFDCDevendra@SFDC

Hi,

 

get { return soql + 'order by ' + sortField + ' ' + sortDir + 'limit 20 '; } order by should be preceded with white space.

 

get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }

 

If you dont provide any value to textfields then null would get considered inside a query. Therefore check for null inside a controller to avoid null exceptions.

 

Thanks,

Devendra

This was selected as the best answer
phani_mphani_m

hai.....

 

Now that ERROR msg  discarded, but the problem is records are displaying automatically even though i didn't enterd any values and if  I entered  the value in the input field then it is showing ERROR msg even the record is present there