Friday 11 August 2017

Make custom button calling web-service static method working in Lightning

Hi Folks,

Recently we switched our classic Salesforce edition into Lightning edition.

The custom button calling visual-force page worked fine in lightning but the custom button executing Javascript or calling the Webservice method are not even displayed on the page layout as it's a known thing in Lightning.

We have a custom button calling Webservice method and we have two options to make it working in lightning.
1. Create a lighting component and call it using quick action. (For this we need to enable custom domain)

2.We can call a visual-force page and execute our method using the action attribute.
   (No need to enable custom domain)

Let us see how point 2 will actually work :

Suppose we have following code in the custom button
--------------------------------------------------------------------------------------------------------------------
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}

sforce.apex.execute("SurveySender","sendSurvey",{OppId:"{! Opportunity.Id}"});
alert("Survey Sending Started");


Apex Class :
global class SurveySender {
    webservice static void sendSurvey(Id OppId) {
       system.debug('>>>>>>>>>Method Executed>>>>>>>>>>>'+pOppId);            
    }
}
---------------------------------------------------------------------------------------------------------------------

To work it in lightning :

1. Convert global class as below :

global class SurveySender {

   public Opportunity objOpp;

   global SurveySender(ApexPages.StandardController stdController){
        objOpp = (Opportunity)stdController.getRecord();
   }
   
    public pagereference sendEmail(){
      sendSurvey(objOpp.Id);
      return new pagereference(url.getsalesforcebaseurl().toexternalform()+'/'+objOpp.Id);
    }

   webservice static void sendSurvey(Id OppId) {
       system.debug('>>>>>>>>>Method Executed>>>>>>>>>>>'+OppId);            
    }
}


2. Create a Visualforce page with a standard controller :

<apex:page standardController="Opportunity" extensions="SurveySender"
   action="{!sendEmail}">
</apex:page>

3. Convert custom button behavior to Display in existing window without sidebar or header
    Select content source to the Visualforce page created in step 2.

4. Now the button will behave same like the webservice method.


Happy Coding !!!!



6 comments:

  1. The best E-Commerce Platform that helps you to Sell Online, on Facebook, or in person. Customize the Ecommerce site and Software as per your business needs with ease.
    Hire shopify developer for Custom Ecommerce Software for Your Needs

    ReplyDelete
  2. Thanks for sharing information , But how can we show alert ?

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. this is so very help full with me thank you

    ReplyDelete
  5. does this still work? follow this and the button is not available

    ReplyDelete