[SalesForce] 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

Best Answer

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.

PS

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", "https://c.na15.visual.force.com/apex/Target");
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "ids");
    hiddenField.setAttribute("value", ids.join(','));
    form.appendChild(hiddenField);
    document.body.appendChild(form);
    form.submit();
} 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.

Related Topic