Wild Card Searching SOQL in Salesforce
Requirement: Data Filter Based on any Character
Page Code
<apex:page controller="wildcardController">
<apex:form >
<apex:pageblock >
<apex:inputtext value="{!inputtext}"/>
<apex:commandbutton value="Search" action="{!searchRecords}" reRender="ss,msgid"/>
</apex:pageblock>
<apex:pageblock id="pbId">
<apex:pageblocktable value="{!accList}" var="acc" id="ss">
<apex:column value="{!acc.name}"/>
<apex:column value="{!acc.accountnumber}"/>
</apex:pageblocktable>
</apex:pageblock>
<apex:outputLabel value="{!str}" id="msgid"></apex:outputLabel>
</apex:form>
</apex:page>
Class Code
Public class wildcardController {
Public string inputtext{get;set;}
Public List<account> accList{get;set;}
public string str{get;set;}
//Public boolean flagshow{get;set;}
Public wildcardController(){
//flagshow = false;
}
Public void searchRecords(){
// flagshow = true;
accList = database.Query('select name,accountnumber from account where name like '+'\''+'%'+inputtext+'%'+'\'');
system.debug('******'+accList.size());
if(accList .size() ==0)
{
str= 'No Data Found based on the Search Criteria.';
}
else
{
str= '';
}
}
}
Snapshot
Page Code
<apex:page controller="wildcardController">
<apex:form >
<apex:pageblock >
<apex:inputtext value="{!inputtext}"/>
<apex:commandbutton value="Search" action="{!searchRecords}" reRender="ss,msgid"/>
</apex:pageblock>
<apex:pageblock id="pbId">
<apex:pageblocktable value="{!accList}" var="acc" id="ss">
<apex:column value="{!acc.name}"/>
<apex:column value="{!acc.accountnumber}"/>
</apex:pageblocktable>
</apex:pageblock>
<apex:outputLabel value="{!str}" id="msgid"></apex:outputLabel>
</apex:form>
</apex:page>
Class Code
Public class wildcardController {
Public string inputtext{get;set;}
Public List<account> accList{get;set;}
public string str{get;set;}
//Public boolean flagshow{get;set;}
Public wildcardController(){
//flagshow = false;
}
Public void searchRecords(){
// flagshow = true;
accList = database.Query('select name,accountnumber from account where name like '+'\''+'%'+inputtext+'%'+'\'');
system.debug('******'+accList.size());
if(accList .size() ==0)
{
str= 'No Data Found based on the Search Criteria.';
}
else
{
str= '';
}
}
}
Snapshot
Thanks
Sumit Shukla
Comments
Post a Comment