function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hariom Chaudhary 1Hariom Chaudhary 1 

I want to create a VF page which in which i can search all accounts dyanamically without using button. It should work on pressing a key from keyboard. How can i achieve this. Help me out

Best Answer chosen by Hariom Chaudhary 1
Khan AnasKhan Anas (Salesforce Developers) 
Hi Hariom,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="SearchOnKeyupC">
    
    <script>
    function search(){
        ApexMethod();
    }
    </script>
    
    <apex:form >
        <apex:actionFunction name="ApexMethod" action="{!searchAccounts}" reRender="accountTable"/>
        <apex:pageBlock >
            
            <apex:outputText >Account Name</apex:outputText>
            <apex:inputText value="{!name}" onkeyup="search();"/>
            <apex:pageBlockSection columns="3">
                
                <apex:pageBlockTable id="accountTable" value="{!accounts}" var="acc">
                    <apex:column value="{!acc.Name}"/>
                    <apex:column value="{!acc.Rating}"/>
                    <apex:column value="{!acc.Phone}"/>
                </apex:pageBlockTable>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class SearchOnKeyupC {
    
    public list<Account> accounts { get; set; }
    public String name { get; set; }
    
    public SearchOnKeyupC(){
        accounts = new list<Account>();
        //accounts = [Select Name,Rating,Phone From Account];
    }
    
    public pageReference searchAccounts(){
        
        string query = '';
        if(name != ''){
            query = 'Select Name,Rating,Phone From Account Where Name like \''+name+'%\'';
            accounts = database.query(query);
        }
        else{
            //query = 'Select Name,Rating,Phone From Account';
            //accounts = database.query(query);
            accounts = new list<Account>();
        }
        return null;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Hariom,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="SearchOnKeyupC">
    
    <script>
    function search(){
        ApexMethod();
    }
    </script>
    
    <apex:form >
        <apex:actionFunction name="ApexMethod" action="{!searchAccounts}" reRender="accountTable"/>
        <apex:pageBlock >
            
            <apex:outputText >Account Name</apex:outputText>
            <apex:inputText value="{!name}" onkeyup="search();"/>
            <apex:pageBlockSection columns="3">
                
                <apex:pageBlockTable id="accountTable" value="{!accounts}" var="acc">
                    <apex:column value="{!acc.Name}"/>
                    <apex:column value="{!acc.Rating}"/>
                    <apex:column value="{!acc.Phone}"/>
                </apex:pageBlockTable>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class SearchOnKeyupC {
    
    public list<Account> accounts { get; set; }
    public String name { get; set; }
    
    public SearchOnKeyupC(){
        accounts = new list<Account>();
        //accounts = [Select Name,Rating,Phone From Account];
    }
    
    public pageReference searchAccounts(){
        
        string query = '';
        if(name != ''){
            query = 'Select Name,Rating,Phone From Account Where Name like \''+name+'%\'';
            accounts = database.query(query);
        }
        else{
            //query = 'Select Name,Rating,Phone From Account';
            //accounts = database.query(query);
            accounts = new list<Account>();
        }
        return null;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Hariom Chaudhary 1Hariom Chaudhary 1
Thank You Sir