Created
August 26, 2014 14:19
-
-
Save tyoshikawa1106/4e2ca6102baf5a730130 to your computer and use it in GitHub Desktop.
SFDC:SOQLのLIKE検索で押さえておきたいポイント
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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