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
Theodoor van Donge 8Theodoor van Donge 8 

Use custom list view button, keeps remembering deselected records


We have made a custom Visualforce:
 
<apex:page id="CloseCases" standardController="Case" recordSetVar="cases">

    <apex:repeat value="{!cases}" var="case">
        {!case.CaseNumber}
        <br/>
    </apex:repeat>

</apex:page>


This is connected to a custom button which is part of a listview. If we select records in the listview they are correctly displayed in the visualforce page. But when we deselect records they are still visibile in the visualforce.
Best Answer chosen by Theodoor van Donge 8
Theodoor van Donge 8Theodoor van Donge 8
Ok, solved it. 

It works like this; the Visualforce page needs to be modified like this: 
<apex:page id="CloseCases" standardController="Case" recordSetVar="cases" extensions="CloseCasesExtension">

    <apex:repeat value="{!selected}" var="case">
        {!case.CaseNumber}
        <br/>
    </apex:repeat>

</apex:page>

And you need a additional extension: 
public with sharing class CloseCasesExtension {

  private ApexPages.StandardSetController controller;

  public CloseCasesExtension(ApexPages.StandardSetController controller) {
    this.controller = controller;
  }

  public Case[] getSelected() {
    return (Case[]) controller.getSelected();
  }
}

So the takeway for all of this: the recordSetVar does contain the whole listview, not only the selected records.

 

All Answers

Umesh RathiUmesh Rathi
Hi,
This is happening because you are not removing those records from your 'cases' collection. You will need to remove the deselected records from your collection. Add the records if they are selected and remove the records if the are deselected. 
Hope this helps!
Theodoor van Donge 8Theodoor van Donge 8
Hi,

Thanks for replying! Well, the deselection is not something i have build, but just happens in the standard listview. 
So to reproduce it would be like:
  1. Go to the cases list view
  2. Select one case
  3. Click the custom list view button, that points to the Visualforce page
  4. You now can see that there is one selected case in the Visualforce page (so far so good)
  5. Go back to the cases list view
  6. Select another case record
  7. Click the custom list view button, that points to the Visualforce page
  8. And now you see that there are mysteriously two case records selected in the Visualforce page

This is all done in Lightning btw. 
Theodoor van Donge 8Theodoor van Donge 8
Ok, solved it. 

It works like this; the Visualforce page needs to be modified like this: 
<apex:page id="CloseCases" standardController="Case" recordSetVar="cases" extensions="CloseCasesExtension">

    <apex:repeat value="{!selected}" var="case">
        {!case.CaseNumber}
        <br/>
    </apex:repeat>

</apex:page>

And you need a additional extension: 
public with sharing class CloseCasesExtension {

  private ApexPages.StandardSetController controller;

  public CloseCasesExtension(ApexPages.StandardSetController controller) {
    this.controller = controller;
  }

  public Case[] getSelected() {
    return (Case[]) controller.getSelected();
  }
}

So the takeway for all of this: the recordSetVar does contain the whole listview, not only the selected records.

 
This was selected as the best answer