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

How to use Lightning Component to Replace Javascript Button

HI Friends, 
I got stricked please do help. 
We have custom execute javascript button which used to call third party api and using in classic mode. but now we moved to communities so now we are unable to show javascript button on communities pages. because lightning pages not supporting for javascrript buttons. 
Please do help with lightning componenets and vf how to do, because am not familier with lightning. 
Please do help.

Currently we have this java script button 


var result = sforce.apex.execute("GeneratingNewDocLinkForLoanInDB","GeneratingDocLink", {extrnlid:"{!Loan__c.Loan_External_id__c}"}); 

 Thanks in Advance.

Ayush TripathiAyush Tripathi
Hi you can perform this using Quick Action
1 - Object -> Custom, Button and Links Section ->New Action
2-  Create a custom component having a button which would call your apex code.Your Custom Component can be a Custom Visualforce of Lightning Component (in Action Type while creating a Quick Action).
Please Refer the below link for more information
HI Ayush, Thanks for your reply. I tried to use component and controller but getting below error.
his page has an error. You might just need to refresh it.
Action failed: c:Generatedocslinks$controller$Generatedocslinks [sforce is not defined]
Failing descriptor: {c:Generatedocslinks$controller$Generatedocslinks}

                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
	<!--<div class="slds-p-top_small">
   <lightning:button variant="brand" class="buttonWidth" label="New Upload" onclick="{!c.openModel}"/>
</div>	!-->   
	<ui:button label="Generate Document Link" press="{!c.Generatedocslinks}"/>
Generatedocslinks :function(cmp,event){	
var result = sforce.apex.execute("GeneratingNewDocLinkForLoanInDB","GeneratingDocLink", {extrnlid:"{!Loan__c.Loan_External_id__c}"}); 
Please do help

User-added image
Ayush TripathiAyush Tripathi
Hi R R M
That is not the correct way to call apex 
Please Refer the below pattern to call your server side controller

<aura:component controller="GeneratingNewDocLinkForLoanInDB" implements="force:lightningQuickAction,force:hasRecordId">
    <aura:attribute name="firstName" type="String" default="world"/>
    <lightning:button label="Call server" onclick="{!c.echo}"/>

    "echo" : function(cmp) {
        // create a one-time use instance of the serverEcho action
        // in the server-side controller
        var action = cmp.get("c.GeneratingDocLink"); // This is your apex method
        action.setParams({ Id: cmp.get("v.recordId") }); //This is your parameter inside method

        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // Alert the user with the value returned 
                // from the server
                alert("From server: " + response.getReturnValue());

                // You would typically fire a event here to trigger 
                // client-side notification that the server-side 
                // action is complete
            else if (state === "INCOMPLETE") {
                // do something
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                } else {
                    console.log("Unknown error");

        // optionally set storable, abortable, background flag here

        // A client-side action could cause multiple events, 
        // which could trigger other events and 
        // other server-side action calls.
        // $A.enqueueAction adds the server-side action to the queue.