Lightning Simple Apex Method Call:

This lesson teaches about various return type from the apex method and simple processing and display of each

The program explains about aura:If ,conditionalButton click ,Various return type of apex and iteration basics of attributes in lightning

Below is the sample program for the same
apex :
Create and copy paste the apex class and name it as HelloWorldApexMethod

 
/*


 * created by 		:		Shiva RV
 * Date				:		13-10-2018
 * Description		:		ControllerMethods for ApexMethodHelloWorld component
  
*/

//aura enabled mehods must be static and atleast be public or global
public class HelloWorldLightning {

    @AuraEnabled
    public static String sayHelloString()
    {
        return 'Hello World';
    }
    
     @AuraEnabled
   public static Map<String,String> sayHelloSimpleMap()
    {
		Map<String,String> mapData=new Map<String,String>();
        mapData.put('favourite fruit', 'strawberry');
        mapData.put('favourite color', 'black');
        mapData.put('favourite pet', 'dog');
        mapData.put('favourite animal', 'lion');
        return mapData;

    }
    
     @AuraEnabled
    public static Map<id,Account> sayHelloSobjectMap()
    {
        List<Account> accountList=[select id,name from account limit 5];
        Map<Id,Account> accountMap=new Map<Id,Account>(accountList);
        return accountMap;
    }
    
    
    
    @AuraEnabled
    public static List<Account> sayHelloListOfSobject()
    {
         List<Account> accountList=[select id,name from account limit 5];
        return accountList;
    }
    
    @AuraEnabled
    public static List<String> sayHelloList()
    {
        List<String> fruitsList=new List<String>();
        fruitsList.add('lemon');
        fruitsList.add('mango');
        fruitsList.add('banana');
        fruitsList.add('strawberry');
		return fruitsList;
    }
}
Create a lightning component with the below markup code and name the component as
ApexMethodHelloWorld:
<!--


 * created by 		:		Shiva RV
 * Date				:		13-10-2018
 * Description		:		Component to show various return type usage from apex
  
-->
<aura:component controller="HelloWorldLightning">
    <aura:attribute name="isStringData" type="Boolean" default="false"></aura:attribute>
	<aura:attribute name="isStringSimpleMapData" type="Boolean" default="false"></aura:attribute>
    <aura:attribute name="isSobjectMapData" type="Boolean" default="false"></aura:attribute>
    <aura:attribute name="isListOfSobjectData" type="Boolean" default="false"></aura:attribute>
    <aura:attribute name="isListOfStringData" type="Boolean" default="false"></aura:attribute>
    
    <aura:attribute name="StringData" type="String"></aura:attribute>
	<aura:attribute name="StringSimpleMapData" type="Map"></aura:attribute>
    <aura:attribute name="SobjectMapData" type="Map"></aura:attribute>
    <aura:attribute name="ListOfSobjectData" type="List"></aura:attribute>
    <aura:attribute name="ListOfStringData" type="List"></aura:attribute>
	<div class="slds-button-group" role="group">
  	<lightning:button label="fetch String" name="fetch String" aura:id="fetchString" onclick="{!c.fetchRecordBasedType}"></lightning:button> 
  	<lightning:button label="fetch Simple Map" name="fetch Simple Map" aura:id="fetchSimpleMap" onclick="{!c.fetchRecordBasedType}"></lightning:button> 
  	<lightning:button label="fetch Sobject Map" name="fetch Sobject Map" aura:id="fetchSobjectMap" onclick="{!c.fetchRecordBasedType}"></lightning:button>
    <lightning:button label="fetch String List" name="fetch String List" aura:id="fetchStringList" onclick="{!c.fetchRecordBasedType}"></lightning:button>
  	<lightning:button label="fetch Sobject List" name="fetch Sobject List" aura:id="fetchSobjectList" onclick="{!c.fetchRecordBasedType}"></lightning:button>

	</div>
    <br/>
    <aura:If isTrue="{!v.isStringData}">
        {!v.StringData}
    </aura:If>
    <aura:If isTrue="{!v.isStringSimpleMapData}">
        <table>
            <tr>
            <th>Id</th>
            <th>Name</th>
            </tr>
         	<aura:iteration items="{!v.StringSimpleMapData}" var="cus" indexVar="key">
          	<tr>
            <td> {!cus.key}</td> 
            <td>{!cus.value}</td>
           	</tr>
        	</aura:iteration>
         	</table>
    </aura:If>
    <aura:If isTrue="{!v.isSobjectMapData}">
        	<table>
            <tr>
            <th>Id</th>
            <th>Name</th>
            </tr>
         	<aura:iteration items="{!v.SobjectMapData}" var="cus" >
          	<tr>
            <td> {!cus.key}</td> 
            <td>{!cus.value}</td>
           	</tr>
        	</aura:iteration>
         	</table>
        
    </aura:If>
    <aura:If isTrue="{!v.isListOfSobjectData}">
      <ul>
      <aura:iteration items="{!v.ListOfSobjectData}" var="indData">
            <li>{!indData.Id} -->{!indData.Name}</li> 
      </aura:iteration>
      </ul>

    </aura:If>
    <aura:If isTrue="{!v.isListOfStringData}">
      <ul>
      <aura:iteration items="{!v.ListOfStringData}" var="indData">
            <li>{!indData}</li> 
      </aura:iteration>
      </ul>

    </aura:If>
</aura:component>
 Js controller code :
({
	fetchRecordBasedType : function(component, event, helper) 
    {
        //it can be used to get the event's source, in our case it's the button clicked
        var actionEvent=event.getSource();
        //access the button's attribute
		var actionEventName=event.getSource().get("v.name");
        helper.turnOffAllBooleanHelper(component);

	    switch(actionEventName) 
       	{
    		case "fetch String":
               helper.fetchStringHelper(component);
        	   break;
    		case "fetch Simple Map":
               helper.fetchSimpleMapHelper(component);
       	       break;
            case "fetch Sobject Map":
		       helper.fetchSobjectMapHelper(component);
       	       break;
            case "fetch String List":
		       helper.fetchStringListHelper(component);
       	       break;
            case "fetch Sobject List":
		       helper.fetchSobjectListHelper(component);
       	       break;
    		default:
               console.log("error");
	   	}
	},
    
})
Helper :
({
	fetchStringHelper : function(component) {
		 var action = component.get("c.sayHelloString");
         action.setParams({});
         action.setCallback(this, function(response)
         {
         	var state = response.getState();
         	if (state === "SUCCESS") 
         	{
               component.set("v.isStringData",true);
               component.set("v.StringData",response.getReturnValue());
         	}
         	else
         	{
                // do something
         	}    
         });
         $A.enqueueAction(action);
	},
    
     fetchSobjectMapHelper: function(component)
    {
		 console.log("in fetchSobjectMapHelper");
         var action = component.get("c.sayHelloSobjectMap");
         action.setParams({});
         action.setCallback(this, function(response)
         {
         	var state = response.getState();
         	if (state === "SUCCESS") 
         	{
               component.set("v.isSobjectMapData",true);
                var objectData=[];
                var returnValue=response.getReturnValue();
                console.log(returnValue);
                
                for(var propt in returnValue)
                {
                    console.log(propt +"  "+returnValue[propt]);
                //    objectData.push({"Id":response.getReturnValue()[i].Id,"name":response.getReturnValue()[i].name});
                    objectData.push({"key":propt,"value":returnValue[propt].Name});
                }
                console.log(objectData);
               component.set("v.SobjectMapData",objectData);
         	}
         	else
         	{
                // do something
         	}    
         });
         $A.enqueueAction(action);	
    },
    
    fetchSimpleMapHelper: function(component)
    {
         console.log("in fetchSimpleMapHelper");
		 var action = component.get("c.sayHelloSimpleMap");
         action.setParams({});
         action.setCallback(this, function(response)
         {
         	var state = response.getState();
         	if (state === "SUCCESS") 
         	{
               component.set("v.isStringSimpleMapData",true);
                var listOfMapData=[];
                var returnData=response.getReturnValue();
                for(var propt in returnData)
                {
                  listOfMapData.push({"key":propt,"value":returnData[propt]})
                console.log(propt+" "+returnData[propt]);

				}
               component.set("v.StringSimpleMapData",listOfMapData);
         	}
         	else
         	{
                // do something
         	}    
         });
         $A.enqueueAction(action);
    },
    fetchStringListHelper: function(component)
    {
      	 console.log("in fetchStringListHelper");
		 var action = component.get("c.sayHelloList");
         action.setParams({});
         action.setCallback(this, function(response)
         {
         	var state = response.getState();
         	if (state === "SUCCESS") 
         	{
               component.set("v.isListOfStringData",true);
               component.set("v.ListOfStringData",response.getReturnValue());
         	}
         	else
         	{
                // do something
         	}    
         });
         $A.enqueueAction(action);
    },
    fetchSobjectListHelper: function(component)
    {
         console.log("in fetchSobjectListHelper");
  		 var action = component.get("c.sayHelloListOfSobject");
         action.setParams({});
         action.setCallback(this, function(response)
         {
         	var state = response.getState();
         	if (state === "SUCCESS") 
         	{
                console.log(response.getReturnValue());
                var objectData=[];
                for(var i=0;i<response.getReturnValue().length;i++)
                {
                //    objectData.push({"Id":response.getReturnValue()[i].Id,"name":response.getReturnValue()[i].name});
                   objectData.push(response.getReturnValue()[i]);
                }
               component.set("v.isListOfSobjectData",true);
               component.set("v.ListOfSobjectData",objectData);
         	}
         	else
         	{
                // do something
         	}    
         });
         $A.enqueueAction(action);
    },
    turnOffAllBooleanHelper : function(component)
    {
        component.set("v.isStringData",false);
        component.set("v.isStringSimpleMapData",false);
        component.set("v.isListOfSobjectData",false);
        component.set("v.isListOfStringData",false);
        component.set("v.isSobjectMapData",false);

    },
    
})
Below is the hello world App code:
<!--


 * created by 		:		Shiva RV
 * Date				:		13-10-2018
 * Description		:		Hello World Lighting App
  
-->

<aura:application extends="force:slds">
<!--this is the lightning app where you can place html codes , associate js code
		also can embed lightning component to it -->
  
 <!-- apex action method demo -->
    <c:ApexMethodHelloWorld></c:ApexMethodHelloWorld>
</aura:application>
Output:

Key Notes :
*Aura:Iteration is used to loop through list item attribute in lightning
*Map is same as object which works as key value pairs
The above programs show's about different return types of apex and the processing at js controller