Server Side Controller | Salesforce Lightning

SERVER SIDE CONTROLLER

In the last lightning episode, where we discussed global value providers in salesforce lightning.

In this episode, we will discuss how and when we can use the server-side controller in salesforce lightning development.
So let's get started...

As we know, the application which we are using in salesforce lightning is running on a browser.
Any application which you load that might have some data. That data is not on the client-side machine, it is in the server.
Now we need to see if we want to load some data from the server-side controller then how we are actually fetching the data.

Let's consider the following diagram :
=========================================


=========================================

As you can observe in the above diagram, Basically we will write a server-side controller which contains some data.

Then from our client-side controller will call a server-side controller and get the data from it.

*Server-side controller is nothing but apex class which contains some specific data that you need to take care of.
When we use apex class as a server-side controller we have to use with sharing keyword to respect security in salesforce.

For Example :
========================================
public with sharing class SimpleServer
{
}
========================================

Methods inside the server-side controller are static and stateless means they do not maintain state (They don't care who is calling them)

Also, we have to use @AuraEnabled to enable the client and server-side access to the method.

So let's get started with hands-on this :

Server Side Controller : simpleController.apxc
========================================
public with sharing class simpleController
{
 @AuraEnabled
 public static String serverEcho (String firstName
 {
  return('Hello from the server'+ firstName); 
 }
}
=======================================

Lightning Component : serverSide.cmp
=======================================
<aura:component controller="simpleController">
  <aura:attribute name="firstName" 
                             type="string" 
                             default="salesforceKid"/>
<ui:button label ="callServer" press="{!c.echo}"/>
</aura:component>
=======================================

JS Controller : serverSideController.js
=======================================
({
echo : function(cmp, event, helper) {
var action = cmp.get("c.serverEcho");
        action.setParams({
            firstName : cmp.get("v.firstName")
        })
        action.setCallback(this, function(response){
            var state = response.getState();
            if(state === "SUCCESS")
            {
                alert("This is from server ::"+ response.getReturnValue());
            }
            else if(state === "INCOMPLETE")
            {
                //do something 
            }
            else if(state === "ERROR")
            {
             var error = response.getError();
             if(error)
             {
                 console.log("error"+errors);
             }
            }
        });
          $A.enqueueAction(action);
}
})
=======================================

In the above .JS Controller we used action.setcallback, which says that the value which we are getting from the server-side controller is correct or not.

Hence we will get three types of response from the server SUCCESS or INCOMPLETE or ERROR.

At last in .Js Controller we have used $A.enqueueAction(action); which will add server side action to queue.

action.setCallback is use for checking the response coming from action is correct or not if it is SUCCESS, INCOMPLETE, ERROR.

Now it's time to check our code so let's create aura application :

Lightning Application : serverSideApp.app
=======================================
<aura:application extends="force:slds">
    <c:serverSide/>
</aura:application>
=======================================

Output :
=======================================


Click On callServer button


=======================================

WOHOOO !! YOU HAVE JUST COMPLETED LIGHTNING SERVER SIDE CONTROLLER EPISODE
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you
Happy learning ☁️⚡️ (Learn. Help. Share.)
Server Side Controller | Salesforce Lightning Server Side Controller | Salesforce Lightning Reviewed by on Rating: 5

No comments:

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.