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
Kumar__MayankKumar__Mayank 

I want to get Account's name based on their type. How to bind types of account from visualpage to extension to get the respective account's name on page? For ex: if type is "prospect" then it should fetch all account's name having type as prospect.

Agustina GarciaAgustina Garcia
You can create a controller with a method that make a soql filter by type
 
public with sharing class AccountBasedOnTypeController {
    
    private String theType;
    public List<Account> accountList {get; set;}
    
    public AccountBasedOnTypeController()
    {
        theType = 'Prospect';
        accountList = calculateAccounts(theType);
    }
    
    public List<Account> calculateAccounts(String filterByType)
    {
        return [Select Id, Name, Type From Account Where Type = :filterByType];
    }

}

And a visualfoce page with a table showing the fields.
<apex:page controller="AccountBasedOnTypeController">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection >
              <apex:pageBlockTable value="{!accountList}" var="acc">
                   <apex:column value="{!acc.Name}">
                        <apex:facet name="header">Name</apex:facet>
                    </apex:column> 
                    <apex:column value="{!acc.Type}">
                        <apex:facet name="header">Type</apex:facet>
                    </apex:column> 
                </apex:pageBlockTable>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

If you need more fields, just add them on the soql and in the table.

Hope this helps
Agustina
Kumar__MayankKumar__Mayank
Thanks Garcia....But the requirement is that means I need to select type of account from picklist
on visualforce page then with the help of search button I need to get Account names. Can I do it with the help of inputfield?
 
Agustina GarciaAgustina Garcia
Yes !! you can

Use apex:selectList (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectList.htm) tag and populate it with all Account type values. Once the end user selects one of them, use the value to make a soql like I did in the code above. The only difference is that I set a specific valye and you need a dynamic one.