4

I have a list of accounts and i need to also have a ability to remove records from this list.

Now this list sometimes gets in a lot of records which causes the page to reach the heap size limit. To get rid of the heap size issue i was thinking of using the standard set controller with pagination so that we dont get a huge heap size.

Now i know that the list of accounts would not hold huge no of records. But i loose the ability to remove the records from the list. I am currently using command link against each record to remove the record from the list.

<apex:commandLink value="Remove" action="{!remove}" reRender="resultTable" immediate="true">
                 <!-- this param is how we send an argument to the controller, so it knows which row we clicked 'remove' on -->
                <apex:param value="{!s.Id}" assignTo="{!toUnselect}" name="toUnselect"/>
            </apex:commandLink> 

The remove method in controller SearchResult is the List of the account.

public pageReference remove()
{
    Integer count = 0;
    system.debug('to unselect is  ' + toUnselect);
    for(Account d : searchResult){
        system.debug('to unselect and d.Id is  ' + d.Id + '  ' + toUnselect);
        if((String)d.Id==toUnselect){
            system.debug('Found a match');
            removedIds.add(toUnselect);
           searchResult.remove(count);
            break;
        }
        count++;
    }

If i were to use the standard set controller

List<account> searchResult= [SELECT Name FROM Account LIMIT 200];
ApexPages.StandardSetController con = new ApexPages.StandardSetController(searchResult);

i can pass the big list into the into setcontroller and pass from the setcontroller into smaller list by using con.getRecords() method.

How should i modify the remove method so that my list should not hold any of the removed records if i traverse to and fro through different pages.

2
  • Why are you worried about the heap size if you are limiting the List to 200? A List of 200 shouldn't break the heap size limits, I'd think. Is it actually the View State that is breaking? If it really is the heap you can't have that List in memory ever. Jun 18, 2013 at 8:01
  • I just gave 200 as i was hitting the heap size limit. But i would be interested in getting a larger set
    – Prady
    Jun 18, 2013 at 8:13

1 Answer 1

4

If you still query for the large list it will be in memory at some point and you will still break the heap size limit. You would need to use the Database.QueryLocator version of the StandardSetController to avoid that. After each successful remove you will need to query the data back into the QueryLocator.

public ApexPages.StandardSetController setCon { get; set; }

public YourConstructor() {
    refreshStandardSetController();
}

public PageReference remove() {
    // remove from DB or possibly just collect Ids to be removed later
    // depending on your requirements.
    refreshStandardSetController();
}

private void refreshStandardSetController() {
    // save off the current page to attempt
    // to keep the user on the same page after the query...
    Integer currentPage = setCon != null ? setCon.getPageNumber() : 1;

    setCon = new ApexPages.StandardSetController(
        Database.getQueryLocator([
            Select Id, Name
            From Account
            /* Where possible where clause */
            Order by Name
    ]);
    // possibly set the page size to something other than default 20...
    setCon.setPageSize(YOUR_CUSTOM_PAGE_SIZE):

    // keep the user on the same page if possible
    Integer newPage = currentPage <= getTotalPages() ? currentPage : setCon.getPageNumber();
    setCon.setPageNumber(newPage);
}

private Integer getTotalPages() {
    return (Integer) Math.ceil(
        Double.valueOf(setCon.getResultSize()) / setCon.getPageSize());
}

See the StandardSetController docs for more information.

0

You must log in to answer this question.

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