[SalesForce] Populating Selected Records in Standard List Controllers

If you have a page using a standard list controller, like this:

<apex:page standardController="Account" recordSetVar="Accounts">

You can create a custom button for account lists allowing the end user to select records with checkboxes, and then click the button to navigate to that custom page with the selected records available using the {!selected} syntax. That's all well and good, but is there a way to navigate to such a page from another custom page that uses a custom controller?

i.e. MyPage is using MyController which wants to navigate to the page above, passing through a few account IDs to work with.


I've looked at the form data passed through form standard pages to the page and there seem to be multiple parameters called ids which each have a different record ID stored in them. If I enter the page URL in the browser directly, and add multiple parameters called ids (e.g. '/apex/AccountsPage?ids=someacctid&ids=anotheracctid') I can loop over selected and see both accounts. Trying to create a page reference using something like this:

String url = '/apex/AccountsPage?';

for(Id acctId : listOfIds)
    url += 'ids=' + acctId + '&'; 

url = url.left(url.length() - 1)

ApexPages.PageReference pr = new ApexPages.PageReference(url);

Does not work, the system seems to remove the duplicate parameters and just leaves one (I think it's illegal to have multiple with the same name).

So is there a way to do this without creating a custom controller for the second page?

Best Answer

It's a HTTP POST request which is bit hard to replicate with PageReference (designed with GET in mind).

enter image description here

Your options are:

  1. share same controller extension across the pages, the variables will pass naturally. Similar to how the wizard example works.
  2. have a regular <form action="/apex/MyPage" method="post"> instead of <apex:form>
  3. Craft the URL yourself in JavaScript as you've already experimented - /apex/AccountsPage?ids=someacctid&ids=anotheracctid. There might be some JavaScript plugins to simplify that for you, not the least of them being the jQuery's functions around ajax calls.

First one sounds most clean to me but you want it controller-free. So I'd probably go with #2 then.

Related Topic