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
SkwalSkwal 

How to get all selected records from enhancedlist view ?

I have exactly the same issue as in MD Chen post on ideas. How do you get selected records from a enhancedList? Here is the code:

 

Page

 

<apex:page standardController="ProfileHierarchy__c" recordSetvar="profiles" extensions="testController">
  <apex:enhancedlist type="ProfileHierarchy__c" height="200" rerender="c1,c2" />
  <apex:outputText id="c1" value="c1={!c1}" /> <br>
  <apex:outputText id="c2" value="c2={!c2}" /> <br>
</apex:page>

 Controller

 

public class testController {

  public testController (ApexPages.StandardSetController controller){
    li=controller;
  }

  public ApexPages.StandardSetController li{get;set;}

  public Integer getC1(){
    return ((List<ProfileHierarchy__c>)li.getRecords()).size();
  }

  public Integer getC2(){
    return ((List<ProfileHierarchy__c>)li.getSelected()).size();
  }
}

Thank you.

 

 

 

SkwalSkwal

I omitted to mention that the code below always returns 0 for selected records when calling the .getSelected() method. I appreciate any advice that would solve this problem. Thanks.

im_danim_dan

This is a the same as this post

Does anyone know the answer to this question?

trmptrmp

Did anyone here find a solution to this?

 

This idea might help if it were implemented:

 

https://success.salesforce.com/ideaView?id=08730000000g6wLAAQ

schechterschechter

You can get all of the selected records on an enhancedList (regardless of pagination or number or records on the current page) on a visual force page with javascript.

You'll need to find the ID of the 'ListViewport' eg j_id0:j_id4

 

var list_view_port = ListViewport.instances['<your ListViewport's id']>

var selected_records = list_view_port.grid.selModel.selections.keys

 

(keys will be a list of the id of the records.  You can also get 'data' instead of keys (as well as other options) which will have more info about the selected records.)

trmptrmp

Here is what I ended up doing. Create a list view button:

 

{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")}
try {
	if (getRecordIds && testFunc) {
		var ids = {!GETRECORDIDS($ObjectType.Contact)};
		if (!ids || ids.length < 1)
			alert('Please select at least one record.');
		else
			testFunc(ids.join(','));
	}
} catch(ex) {
	alert('This button can only be used from [location].');
}

 

Include a JavaScript function and actionFunction in your Visualforce page:

function testFunc(ids) {
	run(ids);
}

 

<apex:actionFunction name="run" action="{!run}" status="runStatus" rerender="pageMsgs">
	<apex:param value="" assignTo="{!selectedContacts}" name="selContacts" />
</apex:actionFunction>

 

And finally an Apex member and method:

public string selectedContacts { get; set; }

public PageReference run() {
//DO STUFF WITH selectedContacts.split(',')
}

 

Hope this helps others!