Get list of contacts from search layout

When I select multiple clients on this list and click the Email Contacts button on this search page I want to open a visual force page with a special email form on it. How do I get the ids of all of the Contacts that I selected on this page onto the new page?

The Add to Campaign button clearly redirects to a page and has a list of all of the contacts that I have selected so that I can add them to a campaign. I just want to know if I can mimic this ability in my own pages.

If I were doing execute javascript, then I could use the {! GETRECORDIDS (…) } function, but this doesn’t seem to work on APEX pages. Only in the on click execute javascript.

=== EDIT ===

Sorry, this above part was not clear… what I’m saying is that I know I can use {! GETRECORDIDS (…)} to get the records that I selected, but what I want to do is load a visual force page with the open in existing window functionality. Then I want to get the data and access it on that apex/visualforce page.

enter image description here


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can add a “List Button” set to “Execute JavaScript” with this JavaScript:

var ids = {!GETRECORDIDS($ObjectType.Contact)};
if (ids.length) {
    if (ids.length <= 100) {
        window.location = '/apex/MyPage?ids=' + ids.join(',');
    } else {
        alert('Select 100 or less');
} else {
    alert('Select one or more Contacts');

Your Apex page (MyPage here) then just has to split the “ids” parameter to get the set of selected ids.

The limit of 100 selected items is imposed because 2k characters is generally considered the longest URL that works safely everywhere.


To address the comment that more than 100 selected objects may need to be supported, here is how to use a POST instead of a GET:

var ids = {!GETRECORDIDS($ObjectType.Contact)};
if (ids.length) {
    var form = document.createElement("form");
    form.setAttribute("method", "POST");
    // This must be the full URL
    form.setAttribute("action", "");
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "ids");
    hiddenField.setAttribute("value", ids.join(','));
} else {
    alert('Select one or more Contacts');

A bit more detail in How to pass a large number of selections from a standard list view to a Visualforce page.

Method 2

After months of trying to hack around this issue my co-worker discovered how to do this. Just make the button Content Source a Visualforce Page and set Content to the page that you want:

Buttons, Links, and Actions for a standard object

then add this code to your controller:

List<Contact> passedContacts = [
    SELECT Id,name,email 
    FROM Contact 
    WHERE Id IN :setCon.getSelected()];

And to your page:

<apex:page standardController="Contact" extentions="YourController" ...

That code will return all of the contacts (or whatever standard object you are using) inside YourController. Then you can just use the list of contacts.

There’s always an easy answer somewhere. 🙂

Warning: You will still have this issue: CurrentPage getParameters() is empty the first time I post to a page. This is a bug on salesforce’s end as far as I can tell, they recently made a patch to fix it, but there is still something wrong on the first time you login after being logged out for a while. I’m going to report it and I’ll post back when that gets fixed all the times. Just telling you in case you run into that issue.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x