1

So I am trying to make a VF Page to display list of account records. We must have pagination and filtering from standard controller. Also we must use custom controller to create methods to alternate between ascending and descending sort on column name using column facet and commandlink. So I can get the pagination and filtering to work by itself. I can also get the column sorting to work by itself. However after extending standard account list controller column sorting functionality breaks. The visualforce page will always display sorted by account Name in ascending order. even though in the controller i have tried to default to sort by phone in descending order just to see if it would change.

Here is my VF PAGE:

<apex:page standardController="Account" recordSetVar="accounts" extensions="fakeAccountsListController">
<apex:form >
    <apex:pageBlock title="Accounts List" id="accounts_list">
        Filter: 
    <apex:selectList value="{! filterId }" size="1">
      <apex:selectOptions value="{! listViewOptions }"/>
       <apex:actionSupport event="onchange" reRender="accounts_list"/>
       </apex:selectList><apex:pageBlockTable value="{! accounts }" var="act">
     <!--  THE ACTIONS IN EACH COLUMN ARE NOT WORKING --><apex:column value="{! act.Name }">
        <apex:facet name="header">
            <apex:commandLink action="{! sortByName }" 
            reRender="accounts_list">First Name
            </apex:commandLink>
        </apex:facet>
       </apex:column>
<apex:column value="{! act.Phone }">
        <apex:facet name="header">
            <apex:commandLink action="{! sortByPhone }" 
            reRender="accounts_list">Phone
            </apex:commandLink>
        </apex:facet> 
       </apex:column> 
<apex:column value="{! act.NumberOfEmployees }">
        <apex:facet name="header">
            <apex:commandLink action="{! sortByNumEmpl }" 
            reRender="accounts_list">Num of Empl
            </apex:commandLink>
        </apex:facet> 
       </apex:column>     
<apex:column value="{! act.Associated_Contacts__C }">
            <apex:facet name="header">
            <apex:commandLink action="{! sortByAssCons }" 
            reRender="accounts_list">Associated Contacts
            </apex:commandLink>
        </apex:facet> 
       </apex:column>
       <apex:column value="{! act.Owner.Email }">
        <apex:facet name="header">
            <apex:commandLink action="{! sortByOwnerEmail }" 
            reRender="accounts_list">Owner Email
            </apex:commandLink>
        </apex:facet> 
       </apex:column></apex:pageBlockTable>
        <!-- Pagination -->
        <table style="width: 100%"><tr>
        <td>
        Page: <apex:outputText value=" {!PageNumber} of {! CEILING(ResultSize / PageSize) }"/>
        </td>            
        <td align="center">
        <!-- Previous page -->
        <!-- active -->
        <apex:commandLink action="{! Previous }" value="« Previous"
        rendered="{! HasPrevious }"/>
        <!-- inactive (no earlier pages) -->
        <apex:outputText style="color: #ccc;" value="« Previous"
        rendered="{! NOT(HasPrevious) }"/>
        &nbsp;&nbsp;  
        <!-- Next page -->
        <!-- active -->
        <apex:commandLink action="{! Next }" value="Next »"
        rendered="{! HasNext }"/>
        <!-- inactive (no more pages) -->
        <apex:outputText style="color: #ccc;" value="Next »"
        rendered="{! NOT(HasNext) }"/>
        </td>
        <td align="right">
        Records per page:
        <apex:selectList value="{! PageSize }" size="1">
        <apex:selectOption itemValue="25" itemLabel="25"/>    
        <apex:selectOption itemValue="5" itemLabel="5"/>
        <apex:selectOption itemValue="10" itemLabel="10"/>
        <apex:selectOption itemValue="50" itemLabel="50"/>
        <apex:selectOption itemValue="100" itemLabel="100"/>
        <apex:actionSupport event="onchange" reRender="accounts_list"/>
        </apex:selectList>
        </td>
        </tr></table>
    </apex:pageBlock>
</apex:form>

Here is my custom controller

public class fakeAccountsListController {
public String sortColumn;
public String orderBy;
public List<Account> results;
public List<Account> getAccountList() {   
    results = Database.query(
        'SELECT Id, Name, Phone, NumberOfEmployees, Associated_Contacts__c, Owner.Email ' +
        'FROM Account ' + 'ORDER BY' + sortColumn + '' + orderBy);

    return results;
}

public fakeAccountsListController(ApexPages.StandardSetController stdCon){
    results = (List<Account>)stdCon.getRecords();
         sortColumn = ' Phone';
         orderBy = ' DESC';
} 

// THESE METHODS ARE FOR ALTERNATING SORT ON CLICK OF FACET. WHEN USING ONLY CUSTOM CONTROLLER WITH VF PAGE
// THEY WORK. WHEN USING STANDARD CONTROLLER WITH CONTROLLER EXTENSION FUNCTIONALITY IS BROKEN.
public void sortByName() {
    this.sortColumn = ' Name';
     if(orderBy == ' ASC'){
        orderBy = ' DESC';
        }
    else{
        orderBy = ' ASC';   
    }
}

public void sortByPhone() {
     if(orderBy == ' ASC'){
        this.sortColumn = 'Phone';
        orderBy = ' DESC';
        }
    else{
        this.sortColumn = 'Phone';
        orderBy = ' ASC';   }
}

public void sortByNumEmpl() {
    this.sortColumn = 'NumberOfEmployees';
     if(orderBy == ' ASC'){
        orderBy = ' DESC';
        }
    else{
        orderBy = ' ASC';}
}

public void sortByAssCons() {
    this.sortColumn = 'Associated_Contacts__c';
     if(orderBy == ' ASC'){
        orderBy = ' DESC';
        }
    else{
        orderBy = ' ASC';   }
}

public void sortByOwnerEmail() {
    this.sortColumn = 'Owner.Email';
     if(orderBy == ' ASC'){
        orderBy = ' DESC';
        }
    else{
        orderBy = ' ASC';   
    }
}   
}

sorry the formatting is off inside my code blocks. I was having trouble with the auto formatting on the stackexchange page but code is still readable.. Thank you for any help.

1 Answer 1

1

Your problem is that you're changing the results list, that is obtained from the getAccountList method. However, you're iterating in your recordSetVar (accounts) which is not affected by that method at all.

What you probably want to do is to roll your own StandardSetController instead of inheriting it the way you're doing it right now.

I've extracted a sample from this page. It's basic, but it will get you on your way

Controller:

public class opportunityList2Con {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
        return (List<Opportunity>) setCon.getRecords();
    }
}

Page:

<apex:page controller="opportunityList2Con">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.CloseDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
2
  • Thank you for your response and also editing my code block. I will get started attempting this solution. It will probably take a while as I am new with this and slow! I have a question about this solution. If I use custom set controller won't I lose ability to use {! filterId} expression and pagination functionalities from the standard controller? Those are necessary to meet my requirements.
    – helpifell
    Mar 20, 2019 at 21:57
  • I am not sure where filterId is coming from, but it doesn't really matter. There is no reason for you to actually remove the recordSetVar or the standardController at all. Just iterate on the custom one you're creating. The object does have a setFilter property that lets you input a listviewFilter. Explore the page I sent and you'll find everything it can do. Mar 20, 2019 at 22:02

You must log in to answer this question.

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