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
Robert Davis 16Robert Davis 16 

Customized Account Search

I am trying to create a Visualforce Page that passes in a string to a SOQL query to create a list of Accounts that match the string, but I not able to get any results

Controller:
 
public class AccountSearch_CC {
    
    public String acctName {get; set;}

    public List<AccountWrapper> accountList {
        get {           
            if (accountList == null) {
                accountList = new List<AccountWrapper>();
                for (Account acct : [SELECT  Name, BillingAddress,  Industry, Vertical_Assignment__c FROM Account WHERE NAME =: acctName ]){
                    accountList.add(new AccountWrapper(acct));
                }
            }
            return accountList;
        }
        set;
    }
    public Map<ID, AccountWrapper> selectedAccounts {
        get {
            if (selectedAccounts ==null) {
                selectedAccounts = new Map<Id, AccountWrapper>();
            }
            return selectedAccounts;
        }
        set;
    }
    public PageReference listAccounts(){
        
        selectedAccounts.clear();
        
        for(AccountWrapper acctWpr : accountList) {
            if(acctWpr.checked) {
                selectedAccounts.put(acctWpr.acct.Id, acctWpr);
            }
        }
        if (selectedAccounts.size()>0){
            return Page.AcctSearch;
        } else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select at least one Account'));
            return null;
        }
        
    }
    
    public List<Account> available{
        get {
            if (available == null) {
                available = new List<Account>([SELECT Name, BillingAddress,  Industry, Vertical_Assignment__c FROM Account
                                              WHERE Id IN : selectedAccounts.keySet()]);
            }
            return available;
        }
        set;
    }
    
    public PageReference newSearch() {
        PageReference newPage = Page.SearchAccountPage;
        newPage.setRedirect(true);
        return NewPage;
    }
    
    public class AccountWrapper {
        public Account acct {get; set;}
        public Boolean checked {get; set;}
        
        public AccountWrapper(Account a){
            acct = a;
            checked = false;
        }
    }

}

Visual Search Page:
 
<apex:page controller="AccountSearch_CC" tabStyle="Account">
    <apex:form >
        <apex:pageBlock title="Account Search">
            <apex:pageMessages >
            </apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!listAccounts}" value="See Available Accounts" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable var="a" value="{!accountList}">
                <apex:column width="25px" headerValue="Select">
                    
                    <apex:inputCheckbox value="{!a.checked}" />
                </apex:column>
                <apex:column value="{!a.acct.Name}"/>
                
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

List of Results Page:
 
<apex:page controller="AccountSearch_CC">
    <apex:form >
        <apex:pageBlock title="Search for Accounts">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton action="{!newSearch}" value="New Search" />   
                <apex:inputText value="{!acctName}" id="SearchString" maxlength="25" size="30"/>
            </apex:pageBlockButtons>
                <apex:pageBlockTable var="avail" value="{!available}">
                    <apex:column value="{!avail.Name}"/>
            
                </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Any help would be so appreciated. Thanks. 
Best Answer chosen by Robert Davis 16
SandhyaSandhya (Salesforce Developers) 
Hi Robert,

Below is the simple sample code which i used.Thought it could help you.

 
<apex:page standardController="account" extensions="accsearchcontroller">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!acc}" var="a">  
     <apex:column >  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Name}</apex:outputlink>  
     </apex:column>  
     <apex:column value="{!a.id}"/>  
    </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page>
 
public with sharing class accsearchcontroller {  
   public list <account> acc {get;set;}  
   public string searchstring {get;set;}  
   public accsearchcontroller(ApexPages.StandardController controller) {  
   }  
   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();  
   }  
 }

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi Robert,

Below is the simple sample code which i used.Thought it could help you.

 
<apex:page standardController="account" extensions="accsearchcontroller">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!acc}" var="a">  
     <apex:column >  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Name}</apex:outputlink>  
     </apex:column>  
     <apex:column value="{!a.id}"/>  
    </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page>
 
public with sharing class accsearchcontroller {  
   public list <account> acc {get;set;}  
   public string searchstring {get;set;}  
   public accsearchcontroller(ApexPages.StandardController controller) {  
   }  
   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();  
   }  
 }

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
This was selected as the best answer
Robert Davis 16Robert Davis 16
Sandhya,

Thank you so much. What I love about your solution is it is so simple compared to mine. I will post my results when finished. Thanks again.

Robert
Robert Davis 16Robert Davis 16
#kudos