[SalesForce] How to pass the selected list to a lightning component via URLFOR

You can now launch a lightning component from a list view button using the URLFOR() function with a lightning component that implements the lightning:isUrlAddressable interface. As part of the URLFOR() function, you can pass a parameter in the third argument. What I haven't figured out is how to pass the list of selected records.

{!URLFOR("lightning/cmp/c__MyComponent", null, ??? )}

or, is there another way in the component to get those records?

Best Answer

GETRECORDIDS() does not seem to work with URLFOR calling a lightning component.

There is an alternative using a Visualforce page that in turn calls the lightning component, here is the procedure for account list view:

  1. Create a visualforce page of type standard list controller
  2. In visualforce constructor method, retrieve list of selected records in list view using ApexPages.StandardSetController getSelected() method and build a comma separated string accIds of the account ids selected in listview
  3. Define an apex method in <apex:page> action that returns a pageReference to redirect to lightning component URL similar to this /lightning/cmp/c__yourAuraComponentName?c__auraAttributeName='+accIds
  4. Then create a custom button of type list and select the visualforce page as content source
  5. Create lightning component that implements lightning:isUrlAddressable , having an attribute to store the acccount ids auraAttributeName and init handler method
  6. In lightning component controller JS init method, retrieve pageReference attribute from v.pageReference and get list of account ids from myPageRef.state.c__auraAttributeName
  7. Perform any other processing in lightning component

For detailed sample code refer to my answer to a similar question

Related Topic