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 

how to write soql query

Hai............  i am new to salesforce ,

                 I want to know how to write query for the Custom object (applicant__c) having  Expected salary field . now i need to get the values i.e, if the user enter the value 10,000 now i need to get all  the values <= 10.000  of that object.

 

can any one help me

 

OyeCodeOyeCode
select expected_salary__c from Applicant__C
where expected_salary__c <=10000

this should help
phani_mphani_m
Haiiii............... Actually i created on Dynamic search page with the help of blog ( http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/) problem is now I need to search the field value <= the given range, by doing the modifications... to my code here is my code snippet for both apex and visual force.I need some help regarding this wher i need to write the query to meet the criteria public with sharing class SearchapplicantsController { private String soql {get;set;} // the collection of contacts to display Public List applicants {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 SearchapplicantsController() { soql='select First_name__c,Last_name__c,Skill_set__c,Experience__C,Expected_Annual_Salary__c,Location__c,Qualification__c from Applicant__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 { applicants = 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 Skillset = Apexpages.currentPage().getParameters().get('Skill_set__c'); String Experience = Apexpages.currentPage().getParameters().get('Experience__c'); String ExpectedSalary = Apexpages.currentPage().getParameters().get('Expected_Annual_Salary__c'); String Location = Apexpages.currentPage().getParameters().get('Location__c'); String Qualification = Apexpages.currentPage().getParameters().get('Qualification__c'); soql='select First_name__c,Last_name__c,Skill_set__c,Experience__c,Expected_Annual_Salary__c,Location__c,Qualification__c from Applicant__c where Applicant__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 (!Skillset.equals('')) soql += ' and Skill_set__c LIKE \''+String.escapeSingleQuotes(Skillset)+'%\''; if (!Experience.equals('')) soql += ' and Experience__c LIKE \''+String.escapeSingleQuotes(Experience)+'%\''; if (!Expectedsalary.equals('')) soql += ' and Expected_Annual_Salary__c LIKE \''+String.escapeSingleQuotes(Expectedsalary)+'%\''; if (!Location.equals('')) soql += ' and Location__c LIKE \''+String.escapeSingleQuotes(Location)+'%\''; if (!Qualification.equals('')) soql += ' and Qualification__c LIKE (\''+Qualification+'\')'; // run the query again runQuery(); return null; } // use apex describe to build the picklist values public List Qualification { get { if (Qualification == null) { Qualification = new List(); Schema.DescribeFieldResult field = Applicant__c.Qualification__c.getDescribe(); for (Schema.PicklistEntry f : field.getPicklistValues()) Qualification.add(f.getLabel()); } return Qualification; } set; } }
OyeCodeOyeCode
I can barely read your reply,

Please post code using (code button in comment box) this is mess rather
phani_mphani_m

okkk  Thanks for ur reply now i displayed my code ....................

 

 Actually i created on Dynamic search page   with the help of blog (http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/) problem is now  I need to search the field value <= the given range, by doing the modifications...  to my code here is my code snippet for both apex and visual force.I need  some help regarding this
wher i need to write the query to meet the criteria

 

 

Here is my visualforce code:-

 

<apex:page controller="SearchapplicantsController" sidebar="false">
 <script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById("First_Name__c").value,
          document.getElementById("Last_Name__c").value,
          document.getElementById("Skill_set__c").value,
          document.getElementById("Experience__c").value,
         document.getElementById("Expected_Annual_Salary__c").value,
document.getElementById("Location__c").value,
          document.getElementById("Qualification__c").options[document.getElementById("Qualification__c").selectedIndex].value
          );
      }
      </script> 
 
  <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">
 
       
 
      <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="Skill_set__c" value="" />
<apex:param name="Experience__c" value="" />
<apex:param name="Expected_Annual_Salary__c" value="" />
<apex:param name="Location__C" value="" />

          <apex:param name="Qualification__c" value="" />
      </apex:actionFunction>
 
      <table cellpadding="4" cellspacing="4">
      <tr>
        <td style="font-weight:bold;">First Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="text" id="First_Name__c" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Last Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="text" id="Last_Name__c" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Skillset   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="text" id="Skill_set__c" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Experience &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="text" id="Experience__c" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">ExpectedSalary &nbsp;&nbsp;&nbsp;&nbsp;
        <input type="currency" id="Expected_Annual_Salary__c" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Location &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="text" id="Location__c" onkeyup="doSearch();"/>
        </td>
      </tr>

      <tr>
        <td style="font-weight:bold;">Education &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <select id="Qualification__c" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!Qualification }" var="qual">
              <option value="{!qual}"> {!qual}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>
      </table>
 
      </apex:pageBlock>
 
    </td>
        
  </tr>

    </table>
 
    <apex:pageBlock title="SearchResults" mode="edit" id="results">
 
        <apex:pageBlockTable value="{!applicants}" var="applicant">
 
            <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="{!applicant.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="{!applicant.Last_Name__c}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Skillset" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Skill_set__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!applicant.Skill_set__c}"/>
            </apex:column>
      <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Experience" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Experience__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!applicant.Experience__c}"/>
            </apex:column>
      <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="ExpectedSalary" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Expected_Annual_Salary__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!applicant.Expected_Annual_Salary__c}"/>
            </apex:column>
      <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Location" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Location__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!applicant.Location__c}"/>
            </apex:column>


 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Qualification" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Qualification__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!applicant.Qualification__c}"/>
            </apex:column>
 
        </apex:pageBlockTable> 
 
    </apex:pageBlock> 
 
    
  
  
 
  <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}" />           
  </apex:pageBlock>    
 
  </apex:pageBlock>
 
  </apex:form>
 
</apex:page>

Here is my controller code :-

 

public with sharing class  SearchapplicantsController {   
    
    private String soql {get;set;}
    // the collection of contacts to display
      Public List<Applicant__c> applicants {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 SearchapplicantsController() {
            soql='select First_name__c,Last_name__c,Skill_set__c,Experience__C,Expected_Annual_Salary__c,Location__c,Qualification__c from  Applicant__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 {
                   applicants = 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 Skillset = Apexpages.currentPage().getParameters().get('Skill_set__c');
String Experience = Apexpages.currentPage().getParameters().get('Experience__c');  
String ExpectedSalary = Apexpages.currentPage().getParameters().get('Expected_Annual_Salary__c');
String Location = Apexpages.currentPage().getParameters().get('Location__c');
  String Qualification = Apexpages.currentPage().getParameters().get('Qualification__c');
 
    soql='select First_name__c,Last_name__c,Skill_set__c,Experience__c,Expected_Annual_Salary__c,Location__c,Qualification__c from Applicant__c where Applicant__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 (!Skillset.equals(''))
      soql += ' and Skill_set__c LIKE \''+String.escapeSingleQuotes(Skillset)+'%\'';  
   if (!Experience.equals(''))
      soql += ' and Experience__c LIKE \''+String.escapeSingleQuotes(Experience)+'%\''; 
   if (!Expectedsalary.equals(''))
      soql += ' and Expected_Annual_Salary__c LIKE \''+String.escapeSingleQuotes(Expectedsalary)+'%\''; 
   if (!Location.equals(''))
      soql += ' and Location__c LIKE \''+String.escapeSingleQuotes(Location)+'%\'';


    if (!Qualification.equals(''))
      soql += ' and Qualification__c LIKE (\''+Qualification+'\')';
 
    // run the query again
    runQuery();
 
    return null;
  }
 
  // use apex describe to build the picklist values
  public List<String> Qualification {
    get {
      
      
      
      if (Qualification == null) {
 
        Qualification = new List<String>();
        Schema.DescribeFieldResult field = Applicant__c.Qualification__c.getDescribe();
 
        for (Schema.PicklistEntry f : field.getPicklistValues())
         Qualification.add(f.getLabel());
 
      }
      return Qualification;          
    }
    set;
  }
 
}
colemabcolemab

If you want a truely dynamic search tool (i.e. it finds the fields for you - much like the salesforce list view editor), check out my SOQL query component here.   It might be easier than adding fields to a page manually.  Anyway, just a thought.