1

I think I need a pageReference for the updateRecordList. Right now it runs but it won't display the accounts with the selected account type. Can anyone explain to me whats wrong? and how pageReference works.

APEX controller:

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 boolean editSection{get; set;}

    public AccountTypeController()
    {
        filterAccount = new Account();
        recordList = new Account[0];
    }
    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()
    { //this i'm thinking needs to be a pageReference to list the updatedAccounts //but im not sure how to change this
         recordList=[Select Name, Type, Website From Account Where Type = :filterAccount.Type];
    }
    public pageReference edit()
    {  //pretty sure this is incorrect
        String contactId = Apexpages.currentpage().getParameters().get('AccountId');  

        pageReference pageRef = new pageReference(URL.getSalesforceBaseUrl().toExternalForm() + '/' + AccountId + '/e?retURL=' + account.id);  

        return pageRef;  
    }    
}

VFP:

<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="pwPanel"/>

                    </apex:inputfield>

                    <!--<apex:commandButton action="{!edit}" value="Edit"/>
                    <apex:commandButton action="{!save}" value="Save"/>-->

                </apex:pageBlockSection>

            </apex:pageBlock>  

         </apex:outputPanel>

    </apex:form>

</apex:page>
  • Where are you expecting the account list to display on the VF page? There's nothing in what you've posted that would display the recordList. – Thomas Taylor Jul 23 '19 at 15:42
  • below the account type drop down box. I was told action support would be the command to retrieve the updated account list and display it but not sure if theres something im missing. – Tyler Jul 23 '19 at 15:49
  • <apex:actionsupport> will indeed call your controller and update the list. But the VF Page still needs someplace to put it. Try putting <apex:dataList value="{!recordList}" var="account"><apex:outputText value="{!account.Name}"/></apex:dataList> between </apex:pageBlock> and </apex:outputPanel>. It needs to be inside the outputPanel because that's what you're rerendering. Or you could add another pageBlock with a different id and rerender that, and maybe use a pageblocktable to show all the columns you're querying. – Thomas Taylor Jul 23 '19 at 15:56
  • Awesome! Thanks so much. Right when you mentioned no place to put it I was used pageBlockTable but I like how dataList doesn't display anything until you make a selection. But i like the table format. Would i put pageBlockTable inside of Datalist? or how would i do that? – Tyler Jul 23 '19 at 16:08
0

The VF Page needs someplace to put your recordList. A simple option to show that it's working would be an <apex:datalist>:

[...]
</apex:pageBlock>
    <apex:dataList value="{!recordList}" var="account">
        <apex:outputText value="{!account.Name}"/>
    </apex:dataList>
</apex:outputPanel>
[...] 

It needs to be inside the outputPanel because that's what you're rerendering.

Or, you could add another outputPanel with a different id and rerender that, and maybe use a pageblocktable to show all the columns you're querying. Use the rendered attribute to suppress display until there's data in the list:

[...]
<apex:actionSupport event="onchange" action="{!updateRecordList}" rerender="acctList"/>
[...]
         </apex:outputPanel>
         <apex:outputPanel id="acctList">
             <apex:pageBlock title="Account List" rendered="{!recordList.size > 0}">
                <apex:pageBlockTable value="{!recordList}" var="account">
                    <apex:column value="{!account.Name}"/>
                    <apex:column value="{!account.Type}"/>
                    <apex:column value="{!account.Website}"/>                
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</apex:page>

For the rendered attribute to work correctly, you also need to change the initialization of recordList in your constructor to use an empty list, rather than the filterAccount that you're using to get the Type picklist values from:

public AccountTypeController()
{
    filterAccount = new Account();
    recordList = new List<Account>();
}
| improve this answer | |
  • Thanks for the help I got that part working. What the best way to learn apex/vfp? – Tyler Jul 23 '19 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.