+ Start a Discussion

List Buttons with VisualForce



The online help shows how to use List Buttons when the Content Source is "OnClick JavaScript".  It shows this example:



var records = {!GETRECORDIDS($ObjectType.Case)};
var newRecords = [];

if (records[0] == null)
alert("Please select at least one row")
for (var n=0; n<records.length; n++) {
var c = new sforce.SObject("Case");
c.id = records[n];
c.Status = "New";

result = sforce.connection.update(newRecords);


 How can I do something similar in VF/Apex?  In particular, how do we mimic the GETRRECORDIDS functionality?




You would need to construct the URL with the record IDs, and pass that to your page. For example, the JavaScript might look like this:



var records = {!GETRECORDIDS($ObjectType.Case)}; if(records.length<1) { alert("You must choose at least one row."); } else { window.top.location.href = "/apex/myPage?ids="+records.join(","); }

Then, in your controller, include the following code:


public class myController { public myController() { Set<String> myRecords = new Set<String>(); if(ApexPages.currentPage().getParameters().get('ids')<>null) { myRecords.add(ApexPages.currentPage().getParameters().get('ids').split(',',-1)); } } }

At the point where you add the record IDs to myRecords, you can then perform a query on them to find all of the records you would like to query from. Use that in your query string, then do whatever else you'd like to do to them (update, display in a list, or whatever your intent is).




What's wrong with this approach?



<apex:page standardController="Lead" recordSetVar="leads"> <apex:dataList value="{!selected}" var="l"> {!l.name} </apex:dataList> </apex:page>


 Then bind this page to your custom list button for lead?


You can add a controller extension to this page if you need to augment with Apex.


Daniel BallingerDaniel Ballinger

Hi Andrew,


Could this approach be combined with an apex:iframe to pass the Multi-Record  Selection Id's on the src query string (with a suitable delimiter)? Or do I still need to use a controller extension to format the src URL?


As you found in my blog post, I'm trying to find a way to pass the selected ids off to an external site in an iframe.