function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KilvatiKilvati 

How to read multiple request parameters with the same name?

I have created a custom button on a list page which will send the ids of the selected objects to a custom page with custom controller. In this controller I want to read either all the ids of the objects or the objects themselves.

 

ApexPages.currentPage().getParameters().get(...) only returns one of the parameter values as String. How on earth am I supposed to get all the parameter values?

Best Answer chosen by Admin (Salesforce Developers) 
KilvatiKilvati

I found the solution. It can be fixed using javascript linked to the button:

 

var ids = document.getElementsByName("ids");
var idString = "";
for(var c=0; c < ids.length; c++) {
  var id = ids[c];
  if(id.checked) {
    idString += ","+id.value;
  }
}
document.location = '/apex/GroupPropose?ids='+idString.substring(1);

This will not work properly when there are multiple lists on a page, for example on related list page. It will then post all selected elements, not only the one with a button on it. You will need to check in the controllor you post to, if all ids are pointing to the proper sobject type.

 

All Answers

Jon Mountjoy_Jon Mountjoy_

getParameters() returns a Map<string><string>.

 

You can just use the methods of Map to iterate over the parameters.

Kevin SwiggumKevin Swiggum

I can't give you a direct answer to your question...but I can suggest an alternative route. 

 

Take a look at Jeff Douglas' blog article 

 

There's more in this article than what you need, but I'll refer you to the CategoryWrapper class that pairs an sObject with a boolean "checked" field. When setting up the controller to build your list of values, you can return a list of wrapper classes rather than a list of sObjects...and then your VF Page can refer to that wrapper class and you can bind an apex:inputCheckbox to the "checked" property from the wrapper class (See line 27 of the Category_Search VF page). 

 

The result: upon form submit (in your controller's action method), you can loop through your list of wrapper classes and look for any items where checked=true (i.e., the user clicked the checkbox). See code starting on line 56 of the CategorySearchController class.

 

it's a longer path than what you're looking for, but in the end, I've found it to be more useful since I don't have to take a set of checked IDs and go back to find the sObject that matches that ID...since the checked property and sObject are already paired together in the wrapper. It also adds to the security of your app since you're using Force.com's built in security model rather than depending on request parameters.

 

Hope that helps.

KilvatiKilvati

Kevin,

 

Sadly this is not the solution, because my form is made by a list page:

 

In the screenshot above I want to select multiple elements and then use the "Group Propose" button to send them all to my own page. The problem is that for each selected element, the object id is passed as parameter "ids".

 

The parameter map does only hold one of the elements if multiple are selected. It would be silly if I had to create a whole new page for the list.

KilvatiKilvati

I found the solution. It can be fixed using javascript linked to the button:

 

var ids = document.getElementsByName("ids");
var idString = "";
for(var c=0; c < ids.length; c++) {
  var id = ids[c];
  if(id.checked) {
    idString += ","+id.value;
  }
}
document.location = '/apex/GroupPropose?ids='+idString.substring(1);

This will not work properly when there are multiple lists on a page, for example on related list page. It will then post all selected elements, not only the one with a button on it. You will need to check in the controllor you post to, if all ids are pointing to the proper sobject type.

 

This was selected as the best answer