3

I have the following code -

public PageReference Search()
{
    if(usr.Name != null)
    {
        con = new ApexPages.StandardSetController(Database.getQueryLocator([select Id , name , email , phone, Department from User where name = :usr.Name]));

        // sets the number of records in each page set
        con.setPageSize(5);
    }
    else
    {
        con = null;
    }
    return null ;
}

//Instantiate the StandardSetController
public ApexPages.StandardSetController con{get; set;}    
public List<User> AllSearchUsers
{
    get
    {
        if (AllSearchUsers == null){
            AllSearchUsers = new List<User>();
            if (con != null) AllSearchUsers.addAll((List<User>)con.getRecords());
        }
        return AllSearchUsers;
    }
    set;
}
public void clear(){
    AllSearchUsers.clear();
    system.debug(AllSearchUsers);
}

VF -

<apex:outputPanel layout="block" id="otpNav2">
<apex:pageBlock rendered="{!IF(AllSearchUsers.size != null && AllSearchUsers.size > 0, true , false)}">
<apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!AllSearchUsers}" var="UR">
                    <apex:column headerValue="Name" value="{!UR.Name}"/>
                    <apex:column headerValue="Email" value="{!UR.Email}"/>
                    <apex:column headerValue="Phone" value="{!UR.Phone}"/>
                    <apex:column headerValue="Department" value="{!UR.Department}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton value="Clear" style="border-radius:5px;" action="{!clear}" />
</apex:outputPanel>

I want to clear this list when I click on the clear button - When I implemented the following I am getting this exception - System.FinalException: Collection is read-only -

public void clear(){
 AllSearchUsers.clear();
}

Then, when I changed it to the following, this doesn't work either -

public void clear(){
 AllSearchUsers = new List<User>();
}

How do I clear a read only collection?

1
  • clear() should set AllSearchUsers to null - this will cause the refresh
    – cropredy
    Jul 26, 2016 at 4:14

1 Answer 1

5

Add the records to a separate list, and return that whether or not there is a controller. I find it significantly improves your API to never return a null collection. Also, you should take advantage of having a setter and actually set the value. See the Lazy Load pattern.

public List<User> users
{
    get
    {
        if (users == null)
        {
            users = new List<User>();
            if (con != null) users.addAll(con.getRecords());
        }
        return users;
    }
    private set;
}

You can also clone the List if you prefer, though this strategy is more verbose unless you ignore the null collection axiom.

public List<User> users
{
    get
    {
        if (users == null)
        {
            if (con != null) users = con.getRecords().clone();
        }
        return users;
    }
    private set;
}
3
  • Thank you for the feedback. I have made the required changes but when I call users.clear(); I do not get the read only collection exception but it does not clear the list. Jul 26, 2016 at 1:07
  • What leads you to the conclusion that the list is not clearing? Are you sure it's not a rendering issue? @user_obiwan Please edit your post to clarify.
    – Adrian Larson
    Jul 26, 2016 at 1:13
  • I have updated the code and I am rendering the entire block. I have a debug statement after .clear() and I can still see the values in my debug log. Jul 26, 2016 at 1:36

You must log in to answer this question.

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