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
crop1645crop1645 

List Controller, sorting, and the {!selected} VF value

Scenario: mass edit selected items in a related list (Opportunities of an Account) - how to get the mass edit page to sort same as selected items?

 

Step 1: Select using check boxes 3 Opportunities in the Account related list of Opportunities

Oppo Mass Edit

Step 2: Display the mass edit page

oppo mass edit 2 of 2

 

RESULT: the mass edit page is not sorted in same order as the related list (Month 02 - Month 01 - Month 03 rather than Month 03 - Month 02 - Month 01)

 

Why would this be?

 

Here is the VF code for the page:

<apex:page standardController="Opportunity" recordSetVar="opps">
<script>function setFocusOnLoad() {}</script> <!-- disables focus from going to first field calendar picker -->
<apex:form >
    <apex:sectionHeader title="Change fields on monthly billing opportunities"/>
    <apex:pageBlock mode="edit">
        <apex:pagemessages />
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Change to new value">
            <apex:inputField value="{!Opportunity.Opportunity_Source__c}"/>
            <apex:inputField value="{!Opportunity.NextStep}"/>
            <apex:inputField value="{!Opportunity.Remarks__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Changes apply to these selected items">    
            <apex:pageBlockTable value="{!selected}" var="o" cellspacing="20px">
                <apex:column value="{!o.name}"/>
                <apex:column value="{!o.stagename}"/>
                <apex:column value="{!o.closeDate}"/>
                <apex:column value="{!o.amount}"/>
                <apex:column headerValue="Service Start Date">
                    <apex:inputField value="{!o.SnS_Contract_Start_Date__c}"/>
                </apex:column>    
                <apex:column headerValue="Service End Date">
                    <apex:inputField value="{!o.ContractEndDate__c}"/>
                </apex:column>
            </apex:pageBlockTable>        
        </apex:pageBlockSection>
    </apex:pageBlock>   
</apex:form>

</apex:page>

 

So, here is the detailed question:

 

A. The pageBlockTable uses the {!selected} value which maps to the getSelected() method on the StandardSetController so it correctly picks up the three selected items. There is no controllerExtension.

 

B. The VF documentation Winter 12 version pg 68 states: "When you are using a standard list controller, the returned records sort on the first column of data, as defined by the current view, even if that column is not rendered."  Well, wouldn't the current view be the related list Account>Opportunities? and that first column is Opportunity Name so why is the pageBlockTable not sorted by Opportunity.Name?  It appears to sort randomly as if out of a map.

 

I realize this could be solved with APEX controllerextension but fidelity to the selected items should be preserved without recourse to APEX.

 

Thoughts?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Q1 - I'm afraid you can't rely on that ordering.  The standard list controller derives ordering from list views rather than how the data was presented on the previous page.  A custom controller will allow you to define the sort order and make sure it is consistent each time you access the page.  There's a supersort example (in a code share, I think) which may be a good starting point for this.

 

Q2 - that was based on the visualforce docs saying that the it will use the last viewed list view if no id is supplied.  From your experience it sounds like there's no ordering at all.  Its probably the default order the records are returned from the database or something like that.  Unfortunately this stuff goes on under the hood so we don't have the exact details of how it works.

All Answers

bob_buzzardbob_buzzard

The current view refers to the list view that may be specified to the page as an id.  If you don't specify this, it will default to the last list view that you accessed for opportunities.  The sort order of related lists is defined in the page layout, so the controller wouldn't have access to that.

crop1645crop1645

Bob --

 

I'm not sure I understand your response --

 

Q1 - the related list sort order is defined by page layout - agreed but 'list controller has no way to know that order' so it can't use it in the records presented by {!selected} ?

 

If YES, then am I stuck writing a controllerExtension?

 

Q2 - if the sort order of {!selected} defaults to the last displayed Opportunity List View, then I tried that and it doesn't work. I went to Opportunity tab, clicked a View that was sorted by Opportunity.Name, then went back to my related list, checked the boxes, and clicked my mass edit button -- the order presented in the {!selected} list is random

 

Thanks

bob_buzzardbob_buzzard

Q1 - I'm afraid you can't rely on that ordering.  The standard list controller derives ordering from list views rather than how the data was presented on the previous page.  A custom controller will allow you to define the sort order and make sure it is consistent each time you access the page.  There's a supersort example (in a code share, I think) which may be a good starting point for this.

 

Q2 - that was based on the visualforce docs saying that the it will use the last viewed list view if no id is supplied.  From your experience it sounds like there's no ordering at all.  Its probably the default order the records are returned from the database or something like that.  Unfortunately this stuff goes on under the hood so we don't have the exact details of how it works.

This was selected as the best answer
LAMCORPLAMCORP

Hi Bob,

 

Do you have any examples or references to controller code & test that will sort the records in VF page?

 

Would like to build this in if possible.

 

Thanks in advance.

 

(Just read the supersort part.. skim reading! searching)