March 26, 2014

How to get selected records from list view in Salesforce?

Visualforce page:

<apex:page standardController="Member__c" extensions="MemberExt" recordSetVar="Member">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!memList}" var="M">
            <apex:column value="{!M.Name}"/>
            <apex:column value="{!M.Age__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Apex Controller:

public class MemberExt {
    public List<Member__c> memList {get;set;}
    private ApexPages.StandardSetController standardController;
    private Set<Id> memIds = new Set<Id>();

    public MemberExt(ApexPages.StandardSetController standardController){
        this.standardController = standardController;
        memList = new List<Member__c>();
        for (Member__c mem : (List<Member__c>)standardController.getSelected()){ 
            memIds.add(mem.Id);
        }
        memList = [SELECT Name, Age__c FROM Member__c WHERE ID IN: memIds];
    }
}

Custom Button:


Output:



6 comments:

  1. I am getting all records instead of the selected, what could be the issue here?

    ReplyDelete
    Replies
    1. Make sure you are using ApexPages.StandardSetController standardController and getSelected().

      Delete
  2. I am doing it the same , i just wanted to fetch the origin of the selected cases but i am getting the ids of the selected cases,please help

    ReplyDelete
    Replies
    1. Use SOQL and fetch the origin values with the ids. It may be due to this issue - https://trailblazer.salesforce.com/issues_view?id=a1p300000008XLSAA2

      Delete
    2. I am fetching the origin only with the soql but still it is giving back the ids

      Delete
    3. Put some debug logs and check. If you query Case Origin, then it should fetch. Add additional fields also and check.

      Delete