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
R R MR R M 

How to use Lightning Component to Replace Onclick Javascript Button Event

HI folks, 

I have Onclick javascript button in Classic and now i need use this button lightning component but unfortunately iam getting error. please help. 

Original Onclick javascript button code is:  

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 

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

------------------------
i have tried use lightning component. 
Component 

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >

	<ui:button label="Generate Document Link" press="{!c.Generatedocslinks}"/>
</aura:component>


Controller ​

({
Generatedocslinks :function(cmp,event){	
var result = sforce.apex.execute("GeneratingNewDocLinkForLoanInDB","GeneratingDocLink", {extrnlid:"{!Loan__c.Loan_External_id__c}"}); 
location.reload();
}
})


Output​

User-added image

sfdcMonkey.comsfdcMonkey.com
Hi can you share you GeneratingNewDocLinkForLoanInDB class as well
thanks
R R MR R M

Hi piyush, 
Please check my Apex class

global class GeneratingNewDocLinkForLoanInDB
{
    public static string accToken;
    public static string accsToken;
    public static String expireTime;
    public static String tokenType;
    public static String refToken;
    public Static blob response;
    public static String endpointurl;
    public static string ResponseString;
    public static string blobResult; 
   
    //public static Loan_Doc_Details__c iLDetails = new Loan_Doc_Details__c(); 
    public static list<Loan_Doc_Details__c> lstUpdate = new list<Loan_Doc_Details__c>();
    public static long str;
    
       
    global static void returnToken() 
    {    
        //Custom Settings
        Generate_Loan_Doc_Url__c iCustom = [SELECT Name,ClientId__c,Client_Secret__c,Login_URL__c,Password__c,User_Name__c,Active__c,
                                       EndPoint__c FROM Generate_Loan_Doc_Url__c WHERE Active__c = true];
        system.debug('iCustom'+iCustom);
        
        String loginUri = iCustom.Login_URL__c;
        String clientId = iCustom.ClientId__c;
        String targetString = iCustom.Client_Secret__c;
        String clientSecret = EncodingUtil.urlEncode(targetString,'UTF-8');
        String username = iCustom.User_Name__c;
        String password = iCustom.Password__c;
        endpointurl = iCustom.EndPoint__c;
        String reqbody = 'client_id='+clientId+'&client_secret='+clientSecret+'&scope=partners_api_sensitive&grant_type=client_credentials';
        System.debug(reqbody);
        HttpRequest req = new HttpRequest(); 
        req.setMethod('POST');
        req.setHeader('content-type','application/x-www-form-urlencoded');
        req.setEndpoint(loginUri);
        req.setBody(reqbody);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug(res.getbody());
        
        //Custom Object - To check the access token expired or not
        Loan_Doc_Details__c iLDetails = [SELECT Id,Name,Access_Token__c,Active__c,Expiry_Time__c,Token_generated_time__c FROM Loan_Doc_Details__c 
                                            WHERE Active__c = true LIMIT 1];
        system.debug('iLDetails'+iLDetails);
        
        if(iLDetails.Token_generated_time__c != null){
        
            str = ((system.now().getTime() - iLDetails.Token_generated_time__c.getTime())/ 1000);
            system.debug(str);
        }
        
        system.debug(iLDetails.Access_Token__c);
        system.debug(iLDetails.Expiry_Time__c);
        
        if(iLDetails.Access_Token__c == '' || iLDetails.Access_Token__c == null || (iLDetails.Expiry_Time__c != null && str > iLDetails.Expiry_Time__c)){
            
            accessToken resp1 = (accessToken)JSON.deserialize(res.getbody(),accessToken.class);
            System.debug(resp1.access_token);
            accToken = resp1.access_token;System.debug(accToken);
            expireTime = resp1.expires_in;System.debug(expireTime);
            tokenType = resp1.token_type;System.debug(tokenType);
            
            //update custom object
            Loan_Doc_Details__c creds = new Loan_Doc_Details__c(Id = iLDetails.Id);
            system.debug('AccessToken length - '+accToken.length());
            creds.Access_Token__c = accToken;
            creds.Expiry_Time__c = decimal.valueof(expireTime);
            creds.Token_generated_time__c = system.now();
            system.debug('*****'+creds.Token_generated_time__c);
            lstUpdate.add(creds);
        
        } 
        else{
            accToken = iLDetails.Access_Token__c;
        }
    }

    Webservice static void GeneratingDocLink(String extrnlid)  
    {    
        returnToken();
        System.debug(extrnlid);

        Loan__c iloan = [SELECT ID,Loan_External_id__c FROM Loan__c WHERE Loan_External_id__c =:extrnlid];
        if(iloan.Loan_External_id__c !=null)
        {
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        req.setMethod('POST');
        system.debug(accToken);
        req.setHeader('Authorization', 'Bearer '+accToken);
        req.setHeader('Content-Type', 'application/octet-stream');
        System.debug('******'+endpointurl);
        String endpoint = endpointurl+'/'+iloan.Loan_External_id__c+'/'+'documents';
        system.debug(endpoint);
        req.setEndpoint(endpoint);
        req.setTimeout(100000);
        res = http.send(req);
        System.debug('******'+res.getbody());
        response = res.getbodyasblob();
        System.debug(Response);
               
        //update token details
        system.debug('*****'+lstUpdate);
        if(lstUpdate.size()>0){
            
            update lstUpdate;
        }      
       }        
    }  
    
    global class accessToken
    {
        public String id;
        public String access_token;
        public String expires_in;
        public String token_type;
    }
}