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
Maggie EmmaMaggie Emma 

Convert javascript to lightning component and apex controller class

Hello. I have the following javascript button I need to convert to a lightning component. I'm running into issues with the Apex class. How do I get the 4 account field values and do the 'if statements'? Once I have those field values, how do I create the Lightning Component controller.js? I've read plenty of articles on this but still can't figure it out, can someone please help me with the acutal code?

Original JavaScript button code - 

var taxID= '{!Account.tax__c}'; 
var accID = '{!Account.Id}'; 
var type = '{!Account.TypeId}'; 
var typeName = '{!Account.TypeName}'; 
if(taxD!=null) 

if(typeName !='Personal'){ 
alert('Post message here.'); 

if(type != '987652930764JKS'){ 
var url= '/apex/VisualForcePage?id=' + accID; 
showSplashView(); 
window.location = url; 

else 

var url= '/apex/VisualForcePage?id=' + accID+'&Personal=true'; 
showSplashView(); 
window.location = url; 


else 

alert('Tax Id required'); 
}

Lightning Component (so far) - 

<aura:component controller="Component_Controller" implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,force:hasSObjectName" access="global">
    <aura:attribute name="tax" type="String"/>
    <aura:attribute name="type" type="String"/>
    <aura:attribute name="typeName" type="String"/>

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    <div class="slds-p-around--x-small slds-align_absolute-center">
           <h2>Post message here.</h2>
    </div>
    <div style="text-align:center">
    <lightning:button label="OK" onclick="{!c.launch}" class="slds-m-bottom-medium"/>
    </div>
</aura:component>

Lightning controller.js (so far) - 

({ 
    doInit : function(cmp) {

        })
    },
    
    launch : function(component, event, helper){
        //on click of "OK", redirect to visualforce page
        
        var urlEvent = $A.get("e.force:navigateToUrl");
        urlEvent.setParams({"url":"/apex/VisualForcePage?id=" + cmp.get("v.recordId") });
        urlEvent.fire();
    } 
})
Ekta KanderaEkta Kandera
<aura:component controller="Component_Controller" implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,force:hasSObjectName" access="global">
<aura:attribute name="Account" type="Object" />
<aura:attribute name="accountFields" type="String[]" default="['Name','tax__c','TypeId,'TypeName']"/>
<force:recordData aura:id="recordHandler"
                    recordId="{!v.recordId}"
                    fields="{!v.accountFields}"
                    targetFields="{!v.Account}"
                    recordUpdated="{!c.handleRecordUpdated}"
                    mode="EDIT"
                     />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    <div class="slds-p-around--x-small slds-align_absolute-center">
           <h2>Post message here.</h2>
    </div>
    <div style="text-align:center">
    <lightning:button label="OK" onclick="{!c.launch}" class="slds-m-bottom-medium"/>
    </div>
</aura:component>
Lightning controller.js
({ 
    doInit : function(cmp) {

        
    },
    handleRecordUpdated : function(component, event, helper){
       //this method is called when page is loaded and brings the information of defined fields.

    },
    launch : function(component, event, helper){
        var account = component.get("v.Account");
        var AccountId = cmp.get("v.recordId") ;
        if(account.tax__c !=null) 
        { 
            if(account.TypeName !='Personal'){ 
                alert('Post message here.'); 
            } 
            if(account.TypeId != '987652930764JKS'){ 
                var url= '/apex/VisualForcePage?id=' + AccountId; 
                showSplashView(); 
                window.location = url; 
            } 
            else 
            { 
                var url= '/apex/VisualForcePage?id=' + AccountId+'&Personal=true'; 
                showSplashView(); 
                window.location = url;
                 
            } 
        } 
        else 
        { 
            alert('Tax Id required'); 
        }
    } 
})

Hi Sydney,

You can try this code.

Thanks.
Mohamed ElgindyMohamed Elgindy
What is error here and when I run Execute Anonymous Apex with editor it's failed to run