Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.