I have a requirement where I need to pass record Id to apex class on click of a custom button(which is placed on the standard page). In order to pass record id, I need to write javascript code, but Salesforce lightning is not supporting javascript button citing the security issue as reason.

can you please let me know an alternative way of achieving my requirement.

2 Answers 2


It really depends on the use case. You haven't told us anything about what the user experience of the button should be. You can't get the exact experience but you can figure out something appropriate for Lightning.

Option 1 - if it's appropriate to have a confirmation dialog: Create a Lightning Component Quick Action and put it on the Lightning layout. The component will have a UI that appears as a modal popup, to confirm the action. Once you confirm, it calls the Apex class (via its Apex controller) and then closes the modal.

Option 2 - if you really want just a one-click action: Create a Lightning Component that can be placed on a Record page, and that has a small UI just looking like a button (e.g. use lightning:button). Clicking that would make a call to its Apex controller that runs the Apex class you want. You will not be able to put this button in the Actions section where all the other buttons are, but you can put it elsewhere on the layout (e.g. at the top of the sidebar).

Could be other creative options too.


You can use Visualforce to accept a list of ID values, which you can then act upon. At a basic level, your page looks like this:

<apex:page standardController="someSobjectType" 
           action="{!actionMethod}" />

And in your class, you need a StandardSetController constructor to get the information you want:

public with sharing class ApexClassToUse {
  sobject[] selectedRecords;
  public ApexClassToUse(ApexPages.StandardSetController controller) {
    selectedRecords = controller.getSelected();
  public void actionMethod() {
    // The selectedRecords list contains the records that were checked

With the page and Apex code in place, you can then create a custom list button that uses a Visualforce page. This feature works both in Classic and Lightning.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .