I am trying to make a page just for practice in which the account name, phone is displayed when entered in the search field.
It was successful but I wanted to make it better and tried to use the LIKE operator so any similar search text is displayed in results, but its not working. I know it has something to do with the set and get method in my class. Please look into the code below and excuse my silly mistakes.
Controller Class - just a heads up i have added two class that i tried, both are same. I don't know why i tried the second. Maybe because that's how i have been using get and set methods all my life. Both don't show expected results, but if I insert a text explicitly in the searchtext variable, like 'test', then all the account name with test in it shows up. I think I am calling the accountname method wrong.
public class accountcheckcontroller{
public string accountname {get;set;}
public list<account> acts {get;set;}
string searchtext = '%' + accountname + '%' ;
public pagereference AcctSearch(){
acts = [Select name,phone from account where Name Like :searchtext];
return null;
}
}
or
public class accountcheckcontroller{
string accountname;
public string getaccountname() {
return accountname;
}
public void setaccountname(string accountname) {
this.accountname=accountname;
}
public list<account> acts {get;set;}
string searchtext = '%' + this.accountname + '%' ;
public pagereference AcctSearch(){
acts = [Select name,phone from account where Name Like :searchtext];
return null;
}
}
the VF page
<apex:page controller="accountcheckcontroller">
<apex:form >
<apex:pageblock >
<apex:pageblocksection >
<apex:inputText id="searchaccount" value="{!accountname}"/> <br/>
<apex:commandButton value="Search Account" action="{!Acctsearch}" /> <br/>
<apex:pageblockTable value="{!acts}" var="a">
<apex:column value="{!a.Name}"/>
<apex:column value="{!a.Phone}"/>
</apex:pageblockTable>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>