function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Lightning Experience and OnClick Javascript Custom Buttons

Curious what the best approach is to replace custom buttons that are currently using OnClick Javascript or URL since they are no longer supported when switching to Lightning Experience. Is it best to replace the classic buttons by building a new buton using the Lightning Design System framework within Lightning components and then adding to the page layout?
I'm curious to find this out as well.
Peter Friberg 26Peter Friberg 26
I agree.

Previously a typical Javascript button would contain some code that created an sObject, set the Id to the current record Id, updated some other fields and performed some sforce.connection update of the record.

Now I would say (to be strictly Lightning conformant) that you need to create a Lightning Component consisting of a ui:button using Ligthning Design System, add a client side Javascript controller with a method pointed to by the "press" attribute on the ui:button. That controller calls a client side helper method routing the asynchronous call to an AuraEnabled Apex controller for server side code managinng the update of the record :)

If you want to stick with the sforce.connection or sforce.apex Javascript libraries you could short cuircuit the server side AuraEnabled Apex class, one test class less to write ;)
You can also invoke apex with Process Builder. If you wanted to skip the button, you could create a field (ie checkbox, input, etc) that behaves like a button (unchecks itself after save).
This is the way we approached it.  It may not be the best approach.

 1.) Create a visual force page.  The only purpose of this page is to launch an APEX class controller that opens a URL.  
<apex:page standardController="Opportunity" extensions="opportunityButton" action="{!autoRun}">  
<apex:sectionHeader title="Open Salesforce Opportunity and Pop Page"/>   
<apex:outputPanel > You tried calling the an Apex Controller from a button.  If you see this page, something went wrong. Print this page and send the error to HDNotifier.  

2.) This is an example controller
public class opportunityButton {
    //Define the Project Object
    Opportunity theOpportunity = new Opportunity();   
    String theOpportunityID; 
    String theAccountID;
    String theURL;
    // Constructor - this only really matters if the autoRun function doesn't work right     
    public opportunityButton(ApexPages.StandardController stdController) {        
        this.theOpportunity = (Opportunity)stdController.getRecord();     
    // Code invoked on page load.      
    public PageReference autoRun()
        String thePageOpportunityId = ApexPages.currentPage().getParameters().get('id'); 
        if (thePageOpportunityId == null) 
            // Display the Visualforce page's error message if no Id is passed over             
            return null;         
    for (Opportunity theOpportunity:[select Id, Accountid from Opportunity where id =:thePageOpportunityId Limit 1]) 
        theOpportunityID = theOpportunity.Id; 
        theAccountID = theOpportunity.Accountid; 
        TheURL = '' + theOpportunityID  + '&OPRTYP=1&OPRACCT='+ theAccountID; 
    // Redirect the user to where ever you want to go.
    PageReference pageRef = new PageReference(TheURL);         
    return pageRef;    

3.) Create a new action on the opportunity.  Associate the visualforce page to the action.  

4.) Drag the Action into the appropriate area on the Opportunity page layout.  

Hopefully Salesforce will add support to Javascript buttons again.  Vote for this idea on the ideas exchange.
Karan KamraKaran Kamra

You can go thru below link , it helped me. Hopefully it does for you as well..

Yes , You can create a Custom button --> Execute Java Script --> and also apex classes. 
Venkatesh Kumar SakthivelVenkatesh Kumar Sakthivel
The logic which you have mentioned works like a charm, however i am struck up with a scenario where i have to open the URL (pagereference) in a different tab. I am not able to use "target" attribute(custom button or link) and "" (javascript). 
Please let me know if you have any idea to do the same.