[SalesForce] Get selected records from Lightning list view custom button: URL addressable

I am trying to select records from list view custom button. I want to pass the selected record ids in the paramaters.

I tried the below and its not working for me.

/lightning/cmp/c__TestURLfor?c__listofAccounts={!GETRECORDIDS($ObjectType.Account)}

I am passing the selected record ids to the listofacccounts attribute in the lightning component. Am I missing anything here.

Here is my lightning component and the controller :

Cmp:

<aura:component implements="lightning:isUrlAddressable">
>     <aura:attribute name="listofAccounts" type="List" />
>     <aura:handler name="init" value="{!this}" action="{!c.onPageReferenceChange}"/>
>       {!v.listofAccounts[0]}. 
</aura:component>

Controller :

({
    onPageReferenceChange: function(cmp, evt, helper) {
        var pageRef = cmp.get("v.pageReference");
        var listofAccounts = myPageRef.state.c__listofAccounts;
        cmp.set("v.listofAccounts", listofAccounts);
        console.log('listofAccounts',listofAccounts);
    }
})

I get this error whenever I click the List view custom URL button

"Invalid request, post must use JSON"

Best Answer

As far as I know, we cannot use {!GETRECORDIDS($ObjectType.Account)} to redirect to a Lightning component via URL.

There is another alternative using a Visualforce page, whereby the VF processes the selected recordids from list view then redirects to the lightning component.

Hope this helps

See working example below for account object

Visualforce page

<apex:page standardController="Account" recordSetVar="accs" extensions="VFC_ProcessAccRecords" action="{!redirectToLC}" />

Extension apex class

public class VFC_ProcessAccRecords {
      public List<Account> selAccLst;
      public String accIds;

      // Constructor
      public VFC_ProcessAccRecords(ApexPages.StandardSetController cntlr){
           selAccLst = cntlr.getSelected(); //get selected records from account list view
           accIds = '';  
           for(Account acc : selAccLst){
               accIds += acc.Id + ','; //build list of ids string concatenated with comma                         
            }
           accIds = accIds.removeEnd(','); 
      } 

      public PageReference redirectToLC(){
            String returnUrl = '/lightning/cmp/c__ProcessListView?c__listofAccounts='+accIds;
            PageReference pgReturnPage = new PageReference(returnUrl);
            pgReturnPage.setRedirect(true);
            return pgReturnPage;
      }

  }

Custom button Create a custom list button with content source "Visualforce" and enable option "Display Checkboxes (for Multi-Record Selection)". Choose the visualforce page created above

Lightning component

<aura:component implements="lightning:isUrlAddressable">
    <aura:attribute name="listofAccounts" type="List" />
    <aura:handler name="init" value="{!this}" action="{!c.onPageReferenceChange}"/>
    <lightning:card title="Account List">
        <aura:iteration items="{!v.listofAccounts}" var="item">
            <p class="slds-p-horizontal_small"> Account Id {!item} </p>
        </aura:iteration>       
    </lightning:card>     
</aura:component>

Controller.js

({
    onPageReferenceChange: function(cmp, evt, helper) {
        var myPageRef = cmp.get("v.pageReference");
        var accs = myPageRef.state.c__listofAccounts;
        console.log('listofAccounts',JSON.stringify(accs));
        cmp.set("v.listofAccounts",accs);
        //split the account ids by comma and continue logic
    }
})