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:

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

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 :

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

Below is a snippet of code for Javascript controller :

Below is the snippet of code for apex controller :





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 2020 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?