Activity Forums Salesforce® Discussions How to make a call to controller method from button in Salesforce lightning?

  • Ajay Prakash

    Member
    January 3, 2018 at 11:17 am

    Hi Portia,

    If you want to call a server side controller method from lightning component then you have to create action in lightning component.Please refer below code-

    Component-DemoComponent

    <aura:component controller ="DemoApexCltr">
    <lightning:button variant="brand" label="Submit" onclick="{!c.getAccountJS}" />
    </aura:component>

    Javascript Controller-

    ({
    getAccountJS : function(component, event, helper) {
    var action = component.get("c.getAllAccount");
    action.setCallback(this,function(response){
    console.log(':::'+JSON.stringify(response.getReturnValue()));
    });
    $A.enqueueAction(action);
    }
    })

    Apex Controller - DemoApexCltr

    public class DemoApexCltr {
    @AuraEnabled
    public static list<Account> getAllAccount(){
    return [select id,name from account limit 10];
    }
    }

     

    • This reply was modified 6 years, 11 months ago by  Ajay Prakash.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos