0

I want to be able to select an account type from a drop down list and be able to view all the accounts of a certain account type. I'm new to apex so if anyone knows any resources for learning that'd be great, I'm already familiar with Java and the basics. Here's what I got so far-

APEX:

public class AccountTypeController 
{
    public string AccountId{get; set;}
    public String sortOrder = 'Type';
    public String recoTypes {get; set;} 
    public Account filterAccount{get; set;}
    public Account[] recordList{get; set;}
    
    public AccountTypeController()
    {
        filterAccount = new Account();
        recordList = new Account[0];
    }
    
    
    public List<SelectOption> getType()
    {
        List<SelectOption> AccountList = new List<SelectOption>();
        for(Account act: [SELECT Name, Type FROM Account])
            AccountList.add(new SelectOption(act.Name, act.Name));

        return AccountList;
      
    }
  
    //for table
    public List<Account> getAccs()
    {
        List<Account> results = Database.query(
            'SELECT Name, Type, Website, BillingCity, BillingStreet, BillingState, BillingPostalcode, BillingCountry, Industry ' +
            'FROM Account '
      );
      return results;
    }
    
  
    public List<SelectOption> Accounts()
    {
        List<SelectOption> AccountList = new List<SelectOption>();
        for(Account act: [SELECT Name, Type, Website, BillingAddress, Industry FROM Account])
            AccountList.add(new SelectOption(act.Name, act.Name));

        return AccountList;
      
    }   
    
    public void updateRecordList()
    {
        recordList=[Select Name, Type From Account Where Type = :filterAccount.Type];
    }
        
//ignore
    public PageReference getRedirect() 
    {
        PageReference pg; //= new pageReference('/apex/'+ AccountId);
        

         if(recoTypes!=null && recoTypes!='-Select New Deal-') 
         {    
         pg = new PageReference('/apex/'+ AccountId);
         }
        else
        {
         //pg = new PageReference();
        }
        pg.setredirect(true);
         return pg;
    }
   
}

VF:

<apex:page controller="AccountTypeController">

    <apex:form >
    
        <apex:outputPanel styleClass="requiredInput" layout="block" id="pwPanel">
        
            <apex:pageBlock title="Filter Accounts" id="pb" mode="edit">
                
                <apex:pageBlockSection >
            
                    <apex:inputfield value="{!filterAccount.Type}" label="Account Type">
                
                       <apex:actionSupport event="onChange" action="{!updateRecordList}" rerender="pb"/>
                   
                    </apex:inputfield>
                    
                </apex:pageBlockSection>
                
            </apex:pageBlock>  
            
        </apex:outputPanel> 
        
    </apex:form>
    
</apex:page>

1 Answer 1

1

You can use an apex:inputField instead:

<apex:inputField value="{!Account.Type}" />

Also, you need to make sure you initialize the variable, or you'll get an error:

// Default constructor
public AccountTypeController() {
    account = new Schema.Account();
}

Note that it's generally a bad practice to name variables after standard sObjects or libraries, as that can make your code more verbose than necessary and/or cause compiler errors. In your case, I'd consider renaming it to "filterAccount", making it obvious that we're using that record as the filter:

public Account filterAccount { get; set; }
public AccountTypeController() {
    filterAccount = new Account();
}

<apex:inputField value="{!filterAccount.Type}" />
6
  • Awesome thanks for the feedback! When i choose an item in the picklist menu how would i get the accounts related to that type to appear?
    – Tyler
    Jul 22, 2019 at 19:51
  • @Tyler <apex:inputField value="{!filterAccount.Type}"><apex:actionSupport event="onchange" action="{!updateRecordList}"></apex:inputField> and you'll need a variable to update with values. Your controller would need something like public Account[] recordList { get; set; } public AccountTypeController() { recordList = new Account[0]; } public void updateRecordList() { recordList = [SELECT Name FROM Account WHERE Type = :filterAccount.Type]; } I know it's quite a lot, you might need a new question if you get stuck.
    – sfdcfox
    Jul 22, 2019 at 20:04
  • Okay thanks I'll work with it and see if I can get it to work
    – Tyler
    Jul 22, 2019 at 20:52
  • Does actionSupport not need rerender? I put all the code in without any errors but its still not updating the page. Also does it need to be in an outputPanel?
    – Tyler
    Jul 22, 2019 at 21:06
  • @Tyler Technically, no, reRender not strictly necessary, nor is outputPanel required. We'd need to see your full code to help you diagnose the situation.
    – sfdcfox
    Jul 22, 2019 at 21:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .