Passing Selected ID to Apex Class using Standard Set Controller

  • 1
  • Question
  • Updated 2 weeks ago
  • Answered
Hi,

I am getting selected records ids but my main concern is In the apex class getselected method is used from Standard set Controller.


So my issue is that what should i pass in URL so that it accepts those selected records ID's because every time i am passing selected record ID's it gives me error message of Invalid ID.


I am trying to make One List View button from one of Apps.


So can you please assist me what we can do for the same?


Thanks.
Photo of Arpit

Arpit

  • 1,514 Points 1k badge 2x thumb

Posted 5 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 56,134 Points 50k badge 2x thumb
Apex Standard Set Controllers look for one or more URL Parameters named ids , and then take the contents of each of these parameters and use this to define their set of selected records. 

So here's an example, say you have a Skuid page that shows a Table of Accounts. You have a Visualforce Page called "MapAccounts" that plots the locations of those Accounts on a Map. So you want to have a Mass Action on your Table that passes the Ids of selected Accounts to your Visualforce Page.

Here's what the JavaScript code for the Mass Action Snippet should be:

// Get the Ids of the selected items as an Array
var selectedItems = arguments[0].list.getSelectedItems();
var idsArray = skuid.$.map(selectedItems,function(item){ 
   return item.row.Id; 
});
// Pass these Ids over to our Visualforce Page
window.top.location = 
    "/apex/c__MapAccounts"
    + "?ids=" + idsArray.join("&ids=")
    + "&retUrl=" + encodeURIComponent(document.location.href);

Let's say on your Skuid Page you select 3 Accounts. This should generate a URL looking something like the following:

https://skuid.na11.visual.force.com/apex/UseAccounts?ids=001G000001bOAAnIAO&ids=001G000000w0pikIAA&ids=001G000000sHLyXIAW&retUrl=https%3A%2F%2Fskuid.na11.visual.force.com%2Fapex%2FAccountList

Notice how there are 3 separate URL Parameters all called ids --- this is what Apex Standard Set Controllers are expecting to find to determine their list of selected records, that is, what records are returned by StandardSetController.getSelected()
(Edited)
Photo of Prabir

Prabir

  • 60 Points
Cool. Worked for me :)
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,700 Points 4k badge 2x thumb
Wow. First snippet I've used and boy does it open up the possibilities.
(Edited)
Photo of Saikat Neogy

Saikat Neogy

  • 60 Points
arguments undefined error gives. Can you tell me the reason?
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 45,308 Points 20k badge 2x thumb
Can't really give you a single reason.  This is a very standard JS error because you are asking for arguments somewhere and not getting any.  Maybe you have code written to be invoked in a snippet that is being called in an INline resource - and therefore no arguments are passed.

All I can reccomend is that you follow the console error into your code and determine where the error originates.  Hopefully its in custom code so you can dig into it and determine why there are no arguments being passed. 
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,026 Points 5k badge 2x thumb
Sprinkle a few console.logs() if you have access to the code.  You can always step through the code using Chrome Developer Tools.  Search the community for hints on debugging JS and using developer tools.
Photo of Adnan S.

Adnan S.

  • 0 Points
I have similar requirement to invoke an existing VF page (displaying list of records using StandardSetController) from my lightning component. In my JS i have constructed the URL as described above but the VF is only displaying the record corresponding to 1st Id passed in the URL.


JS code snippet:

var el = this.template.querySelector('lightning-datatable');
var selected = el.getSelectedRows();
var arrayOfIds = [];
for(let request of selected){
     arrayOfIdsIds.push(request.Id);
}

this[NavigationMixin.GenerateUrl](
{
type: 'standard__webPage',
attributes:
{
url: '/apex/VisualForcePage?ids='+ arrayOfIds.join('&ids=')
}
}).then(generatedUrl =>
{
window.open(generatedUrl);

});

URL generated when it navigates to VF page is 

https://sample--devas.lightning.force.com/lightning/webpage/%2Fapex%2FVisualForcePage%3Fids%3Da1D3L0000000OE4UAM%26ids%3Da1D3L0000000DsRUAU

Is there something wrong with this url?