Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Created August 26, 2014 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyoshikawa1106/4e2ca6102baf5a730130 to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/4e2ca6102baf5a730130 to your computer and use it in GitHub Desktop.
SFDC:SOQLのLIKE検索で押さえておきたいポイント
<apex:page controller="LikeSearchController">
<apex:form id="form">
<apex:pageBlock >
<apex:pageBlockButtons location="bottom">
<apex:commandButton value=" Search " action="{!doSearch}" reRender="form" />
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<apex:inputText value="{!key}" label="{!$ObjectType.Account.Fields.Name.Label}" style="width: 200px;" />
<apex:pageBlockTable value="{!accounts}" var="item">
<apex:column headerValue="{!$ObjectType.Account.Fields.Name.Label}">
<apex:outputField value="{!item.Name}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public class LikeSearchController {
public List<Account> accounts {get; set;}
public String key {get; set;}
public LikeSearchController() {
this.key = '';
}
public void doSearch() {
String searchKey = '';
if (String.isNotEmpty(this.key)) {
searchKey = this.key.replace('%', '\\%') + '%';
String query = 'SELECT Name FROM Account WHERE Name LIKE: searchKey ';
this.accounts = database.query(query);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment