How to Call Server Side Apex Controller Action in Salesforce Lightning Component?

How to Call Server-Side Apex Controller Action from Client-Side Controller in Lightning Component?

Server-side controller method can be invoked by the client-side controller by using the value provider of c. Client-side action can also be invoked by using c syntax in the component markup.

In the component javascript controller cmp.get(“c.serverControllerMethod”) call invoke the ‘serverControllerMethod’ action of apex controller.

Parameters of server side action can be set using the action.setParams() call.

Below snippet of code sets the value of parameter ‘firstParam’ on the server-side method:

action.setParams({ firstParam : cmp.get("v.firstParam")});

A callback action can also be invoked after response has returned from server, using the below snippet of code:

action.setCallback(this, function(response) { ... });

Here, the response parameter of callback function has the response return by the server.

Server-side controller action can be added to the queue to actions to be executed using $A.enqueueAction(action).Enqueued actions will run at the end of the event loop. The lightning framework batches the enqueue action into a single request inspiteof sending a separate request for every action. These actions execute asynchronously and have callbacks.

Let us understand calling of server action from client controller from an example :

Below is a snippet of code for Lightning component markup :

<aura:component controller=”myApexController”>

<aura:handler name=”init” value=”{!this}” action=”{!c.callClient}”/>

<aura:attribute name=”myAttribute” type=”String” default=”Hello World”/>


Here, in the component markup the handler of Init event calling the javascript action ‘callClient’;

Below is a snippet of code for Javascript controller :

callClient : function(component, event, helper) {

var myAttribute = component.get("v.myAttribute");// getting the value of component attribute using c notifier

var action = component.get("c.setAttribute");// calling the server side action using c notifier

action.setParams({ "myString" : myAttribute });// setting the arguments of server action

action.setCallback(this, function(response) {

var state = response.getState();

if (state === “SUCCESS”) {

// Do stuff


else {






Below is the snippet of code for apex controller :

@AuraEnabled // server side action should be auraEnabled

public static void setAttribute(String myString) {

Account acc = new Account(); = myString;

Insert acc;






Leave a Reply


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2018 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?