3

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

2 Answers 2

2

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.

10
  • That's the issue that I'm trying to get around. We may need more then 100 contacts (shouldn't normally, but it should be a possibility), so we can't use a GET parameter. I've tried local storage and other things, but the Add to Campaign built in page just posts that data to their page on the back end. I want to replicate that functionality. Jul 23, 2014 at 16:02
  • @JoshuaHarris Updated the answer with a POST version that should support a pretty much unlimited number of selected objects.
    – Keith C
    Jul 23, 2014 at 18:02
  • The real problem that I am having is that I want to use the Display in existing window function and load from a visualforce page. That should do a post naturally and there should be some way of retrieving the data once I am on that page. Using a form would work, but is kind of a hacky way of getting that behavior. Jul 24, 2014 at 15:32
  • @JoshuaHarris Well the code in the "PS" meets your requirements: see the blog link for how to pick up the posted data in the page. Form submission is what the internet is built out of...
    – Keith C
    Jul 24, 2014 at 17:37
  • Sorry, maybe I wasn't clear in the question, but what I'm trying trying to do is avoid the execute javascript option. I should be able to select the open in existing window and have an apex page, then get the records that I selected on that page. Jul 25, 2014 at 13:28
1

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.

5
  • @KeithC just wanted to let you know that I eventually found the 'proper salesforce' way of doing this. I know you wrote a blog post about it and thought you might want to know about this solution. Oct 3, 2014 at 14:49
  • Joshua, great that you've come back to post the answer. But could you add a bit more detail - I don't quite follow he "make the button change to a visual force page" so perhaps others won't either.
    – Keith C
    Oct 3, 2014 at 21:33
  • @KeithC There are actually some problems with doing this. I will get back to you once we have figured this out. Oct 6, 2014 at 14:31
  • @KeithC I updated the answer. The issue I was worried about is the warning in there. I will update after the bug gets fixed. Let me know if this all makes sense for how to set up the button to pull in the selected contacts. Nov 3, 2014 at 22:32
  • Makes sense - nice.
    – Keith C
    Nov 3, 2014 at 22:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .