4

Visualforce use case

  1. You are working around List View inlineEdit limitation preventing edits on dependent picklists
  2. You create a list button that when clicked, will display all checked rows for mass edits enter image description here

enter image description here

Your VF markup looks like this:

<apex:page standardController="Account" recordSetVar="aList">
<apex:form >
    <apex:pageMessages/>
    <apex:pageMessage title="Select at least one row before clicking Mass Edit"
                        severity="info" rendered="{!selected.size = 0}"/>                                                       
    <apex:pageBlock title="Account Multirow Edit Form">
        <apex:pageBlockButtons location="top">
            <apex:commandButton id="saveBtn" action="{!Save}" value="Save"/>
            <apex:commandButton id="cancelBtn" action="{!Cancel}" value="Cancel"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
    <apex:pageBlock >
        <apex:pageBlockTable id="aTable" value="{!selected}" var="a">  <!--  {!selected} is ssc.getSelected() method result -->
            <apex:column id="Name" value="{!a.name}"/>
            <apex:column headervalue="Geo">
                <apex:inputField id="geo" value="{!a.Geo__c}"/>
            </apex:column>              
            <apex:column headervalue="Region">
                <apex:inputField id="region" value="{!a.Region__c}"/>
            </apex:column>

        </apex:pageBlockTable>
    </apex:pageBlock>  
</apex:form>
</apex:page>

Now, if you want to test the size of the {!selected} list of Accounts using the technique shown here: Is it possible to test list size from a VisualForce page? in order to display a message when the selected list is of size 0 you get a VF compiler error:

Invalid field 'size' for SObject Account

That is, the expression in the rendered= on apex:pageMessage {!selected.size = 0} does not compile.

For grins, I changed the expression to {!selected.isEmpty} and discovered that the list {!selected} is of type VisualForceArrayList - something that is undocumented AFAIK but appears in some use cases (not standardsetControllers) to support a "size" and "empty" property.

OK, so here are the questions:

  1. Is there some other undocumented property that works on standardSetController lists to get the list size?
  2. Why does {!someList.size} or {!someList.empty} work when using related lists within standardControllers but not the implicit list of a standardSetController? They both seem like lists of SObjects to me.
  3. Where is the size or empty property even documented when used in VF list references?
  4. Is there some solution other than coding up an Apex standardSetController Extension? (which I know how to do but obviously would like to use out-of-box features)
10

I devised a technique before now which abuses <apex:variable> elements to cast the list to a string which can then be compared with a string equivalent of an empty array "[]" (some list types seem to generate an empty string instead "" so that's another case to check for). I started using this method because of the difference between getting the size of a 'proper' list in a standard controller and getting the size of a related list.

First do the list itself:

<apex:variable var="v" value="'{!someList}'"/>

Meaning now the variable v has the value '[]'—single quotes included—if it is empty. We need to compare against a string which has the same value, again including the quotes. So far my attempts at escaping quotes directly in the comparison have proven unsuccessful, but we can avoid the need to do so by using a second <apex:variable>, thus:

<apex:variable var="v2" value="'[]'"/>

The upshot is now all we need do is perform a comparison between the two variables to decide whether or not the list has any records.

<apex:outputField value="List is not empty!" rendered="{!v != v2}"/>

To answer your points

  1. Not that I've been able to find
  2. They seem to be treated as different types for some reason
  3. Will get back to you on that, pretty sure I've seen it
  4. See above :)
  • 2
    Superb solution. One modification to your answer though -- turns out that <apex:variable var="vSelectedListAsString" value="{!selected}"/> yields an empty string when no rows are selected from a list view, not an expression of '[]'. So I compared to another apex variable defined as: <apex:variable var="vZeroLenString" value=""/> and my render= works as desired. Curiously, rendered="{!LEN(vSelectedListAsString) = 0}" won't compile as vSelectedListAsString, an apex:variable, is not a String, but an Object. – cropredy Feb 27 '14 at 1:16
  • 1
    Haha, brilliant solution. But sad that we need this. – Willem Mulder Oct 3 '14 at 14:55

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.