2

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"

  • 1
    Please edit your post to include what you've done in the lightning component as it's there you're utilizing isUrlAddressable – Kris Goncalves Dec 24 '19 at 16:34
  • @KrisGoncalves, I have added further information. – pragii Dec 24 '19 at 17:32
  • can you add code for where u r setting pagereference and navigationservice firing? – sdandamud1 Dec 24 '19 at 19:36
  • @sdandamud1 : I am referring to developer.salesforce.com/docs/component-library/bundle/…. So basically I want my custom button on the list view to capture the selected record ids and send it to the lightning component. I see the latest release on URL addresable implements can do it. But not sure if I am doing it right. I am calling the lightning component from my button ( as shown above). – pragii Dec 24 '19 at 23:46
2

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
    }
})
| improve this answer | |
0

You can also achieve the same functionality by embedding the Lightning component on the VF Page.

Set the Values in the VF page Controller in the variable and pass the values on the Lightning Component and that gets stored in the Attributes of the Lightning COmponent.

Then and there u can use it without any much changes

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.