Wednesday, August 27, 2014

Wild card search in soql.

Wild card search in soql.

Use like operator along with % character in where clause of SOQL to implement wild card search. In the following example query returns account records matching input text with account name. %'input string'% in where clause returns all the accounts having the input string anywhere in the account name. For ex.if the input string is 'tech' , query will return account with names 'Willsinfotech' and also 'infotech solutions'

Visualforce Page:

<apex:page controller="WildcardSOQLController" id="pg">
  <apex:form id="fm" >
    <apex:pageblock id="pb" >
        <apex:inputtext value="{!inputtext}" id="inpt"/>
        <apex:commandbutton value=" Search " action="{!searchRecords}" />
    </apex:pageblock>
    <apex:pageblock rendered="{!flagshow}">
      <apex:pageblocktable value="{!accList}" var="acc">
        <apex:column value="{!acc.name}"/>
        <apex:column value="{!acc.accountnumber}"/>
      </apex:pageblocktable>
    </apex:pageblock>
    
    
  </apex:form>
</apex:page>


Controller Class :

Public class WildcardSOQLController {
    Public string inputtext{get;set;}
    Public List<account> accList{get;set;}
    Public boolean flagshow{get;set;}
    
    Public WildcardSOQLController(){
    flagshow = false;
    }    
    
    Public void searchRecords(){
    
      accList = database.Query('select name,accountnumber from account where name like '+'\''+'%'+inputtext+'%'+'\'');
      if(accList !=null && accList.size()>0){
       flagshow = true;
      }
    }
}





2 comments:

Unknown said...

good information.
SalesForce online training

Unknown said...

This helps the readers a lot salesforce Online Training Bangalore

Post a Comment

 
| ,