0

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>
0

You should try keeping your line inside the method. So, that when you will click on button, then the latest value will be taken from UI, and appended with % mark, and then used. Otherwise, you might be getting the searchText variable value as %null% as of now.

public  class accountcheckcontroller
{ 

public string accountname {get;set;}
public list<account> acts {get;set;}

public pagereference AcctSearch(){
string searchtext = '%' + accountname + '%' ;
acts = [Select name,phone  from account where Name Like :searchtext];

return null;
}  
}
| improve this answer | |
  • It is working as expected as well. – Nitish Singhal Jun 1 '17 at 7:38
  • Thanks Nitish, It worked, you are the best man. Umm but i dont understand when it is out of method, why can't it get the value?why is accountname null, why is it inside only it is able to capture the value?,I thought Value was getting set in set method, so i can use this.accountname variable, but how come only when i include in pagereference method, it works? Thanks again – Niknit Jun 1 '17 at 7:50
  • Setter method is only called when request comes from page to controller. Then only the value if textbox gets set into variable. If this helped, please upvote and select as answer. – Nitish Singhal Jun 1 '17 at 8:21
  • i don't see the best answer option, i have up voted, thanks. so when that button is clicked only the request comes and then only the setter method comes in place? right, that's why it wasn't showing any result before because setter wasn't called thus it had the value null. – Niknit Jun 1 '17 at 8:34
  • Exactly.. you are absolutely correct. In front of answer, there is an up arrow to upvote, and correct sign to mark the answer. – Nitish Singhal Jun 1 '17 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.