Mytutorialrack

In this blog post, we will learn about Server side controllers. After we have created a server-side controller in Apex, we can use @AuraEnabled annotation to enable client and server side access to the controller method.
The methods on which you have used @AuraEnabled annotation will be exposed. These server-side actions are not counted against your org’s API limits. But your server-side controller actions in Apex are subject to all the usual Apex limits.
For e.g.:

 public with sharing class ServerSideControllerExample{
 @AuraEnabled
   public static string helloWorld(String name)
    {
         return (‘Hello from the server, ‘+name)
    }
}

 
Whenever you are using @AuraEnabled annotation, your Apex controller should follow some additional guidelines:

  • Methods should be static and also should be marked as public or global. Non-static methods are not supported
  • If a method returns an Object, then instance methods which retrieve the value of the object’s instance field must be public.

How to create an Apex Server Side Controller?

Below are the steps to create Apex Server Controller.
Step 1: Open the Developer Console in Salesforce.
Step 2:Go to File|New|Apex Class.
Step 3: Enter the name of  your server side controller.
Step 4: Click Ok
Step 5: Create a method for each server-side action in the body of the class.
Step 6: Go to  File| Save
Step 7: Open the component which you want to wire to the new Controller class.
Step 8: Use the controller attribute of the <aura:component> tag to wire the component to the controller.
<aura: component controller=”ServerSideControllerExample“>
 

To learn more

Share:

Recent Posts