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
HoysalaHoysala 

Put a button named “Search”, that will be used to search accounts, when accounts found display. If not found show messaging saying no accounts found. Put a Create button on page as well if no records found to create new account.

Put a button named “Search”, that will be used to search accounts, when accounts found display. If not found show messaging saying no 
      accounts found. Put a Create button on page as well if no records found to create new account.
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Suraj,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page standardController="Account" extensions="SearchCreateAccC">
    <apex:form id="searchForm">
        <apex:PageBlock mode="edit">        
            <apex:pageblockSection id="searchBlockSection">
                <apex:pageBlockSectionItem id="searchBlockSectionItem">
                    <apex:outputLabel >Keyword</apex:outputLabel>
                    <apex:panelGroup >
                        <apex:inputtext id="searchTextBox" value="{!searchText}">
                            
                        </apex:inputtext>
                        <apex:commandButton Id="btnSearch" action="{!search}" rerender="renderBlock" status="status" title="Search" value="Search">
                        </apex:commandButton>
                    </apex:panelGroup>
                </apex:pageBlockSectionItem>
            </apex:pageblockSection>
            <apex:actionStatus id="status" startText="Searching... please wait..."/> 
            
            <apex:pageBlocksection id="renderBlock" >
                <apex:pageblocktable value="{!SearchResults}" var="o" rendered="{!NOT(ISNULL(SearchResults))}">
                    <apex:outputLink value="/{!o.Id}">{!o.Name}</apex:outputLink>
                    <apex:column value="{!o.Name}"/>
                </apex:pageblocktable> 
                <apex:commandButton value="Create Account" action="{!createAcc}" rendered="{!isShow}"/>
            </apex:pageBlocksection>
        </apex:PageBlock>
    </apex:form>
</apex:page>

Controller:
public class SearchCreateAccC {
    
    private apexpages.standardController controller {get; set; }
    public Account acc;
    public Boolean isShow {get;set;}
    public List<Account> searchResults {get; set; }
    public string searchText{
        get{
            if (searchText==null) 
                searchText = '';
            return searchText;
        }
        set;
    }
    
    public SearchCreateAccC(ApexPages.StandardController controller){
        this.controller = controller;
        this.acc = (Account) controller.getRecord();
    }
    
    public PageReference search(){
        if(SearchResults == null){
            isShow=true;
            SearchResults = new List<Account>();
        }
        else{
            SearchResults.Clear();
            isShow=false;
        }
        
        String qry = 'Select Id, Name from Account where name like \''+searchText+'\' Order By Name';
        SearchResults = Database.query(qry);
        return null;
    }
    
    public class SearchCreateAccC {
    
    private apexpages.standardController controller {get; set; }
    public Account acc;
    public Boolean isShow {get;set;}
    public List<Account> searchResults {get; set; }
    public string searchText{
        get{
            if (searchText==null) 
                searchText = '';
            return searchText;
        }
        set;
    }
    
    public SearchCreateAccC(ApexPages.StandardController controller){
        this.controller = controller;
        this.acc = (Account) controller.getRecord();
    }
    
    public PageReference search(){
        if(SearchResults == null){
            isShow=true;
            SearchResults = new List<Account>();
        }
        else{
            SearchResults.Clear();
            isShow=false;
        }
        
        String qry = 'Select Id, Name from Account where name like \''+searchText+'\' Order By Name';
        SearchResults = Database.query(qry);
        return null;
    }
    
    public PageReference createAcc(){
        PageReference pr = Page.CreateAccount;
        return pr;
    }
}
}

Visualforce Page to create Account: CreateAccount
<apex:page standardController="Account">
    <apex:form>
        <apex:pageBlock title="Add Account">
            <apex:pageBlockButtons>                   
                <apex:commandButton value="Save" action="{!save}" reRender="liste" />
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>  
            <apex:pageBlockSection title="Account Details" columns="1">            
                <apex:inputField value="{!Account.name}" /> 
                <apex:inputField value="{!Account.site}"/> 
                <apex:inputField value="{!Account.type}"/> 
                <apex:inputField value="{!Account.accountNumber}"/>              
            </apex:pageBlockSection>    
        </apex:pageBlock>
    </apex:form>
</apex:page>



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.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Suraj,

Below code can fulfill your requirements, Hope this will work for you.

Controller :

public with sharing class accsearchcontroller { 
public list <account> acc {get;set;} 
public string searchstring {get;set;} 
public accsearchcontroller( ) { 

public void search(){ 
string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20'; 
acc= Database.query(searchquery); 

public void clear(){ 
acc.clear(); 

}

 Visualforce page:

<apex:page Controller="accsearchcontroller" > 
<apex:form > 
<apex:inputText value="{!searchstring}" label="Input"/> 
<apex:commandButton value="Search records" action="{!search}"/> 
<apex:commandButton value="Clear records" action="{!clear}"/> 
<apex:pageBlock title="Search Result"> 
<apex:pageblockTable value="{!acc}" var="a"> 
<apex:column value="{!a.name}"/> 
<apex:column value="{!a.id}"/> 
</apex:pageBlockTable> 
</apex:pageBlock> 
</apex:form> 
</apex:page>


Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi