Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saicharanreddyk/619f6dc14a2b7ee1b8bcc3c4f49f8684 to your computer and use it in GitHub Desktop.
Save saicharanreddyk/619f6dc14a2b7ee1b8bcc3c4f49f8684 to your computer and use it in GitHub Desktop.
Lightning -- How to invoke the apex class from client-side controller
1. If you want to invoke any apex class in the in the lightning component
then the class should have @AuraEnabled methods or variables in it.
2. How to define a apex class
public with sharing class ClassName{
@AuraEnabled methods
@AuraEnabled variables
}
3. if you want to invoke any apex method in the lightning component then
a.Method should be annotated with @AuraEnabled
b.Method should be public /global
c.Method should be static
d.Method can return any type of value
e.Method can have any type of parameters and any no of parameters
f. syntax :
@AuraEnabled
public static Integer callMe(){
return 10;
}
@AuraEnabled
public static Account callMe(String key){
return [select Name,Phone from Account where name:key limit 1];
}
@AuraEnabled
public static List<Account> callMe(String key){
return [select Name,Phone from Account where name:key ];
}
4. if you want to invoke any variable directly in the component
a. variable should be public
b. varaible should have @Auraenabled
c. It can have any datatype
Example :
@AuraEnabled public String name {set;get;}
@AuraEnabled public Integer age {set;get;}
5. How to invoke the Apex class in lightning component
<!--
<aura:component controller="namespace:className" > : if we have any custom namespace
<aura:component controller="ApexclassName" > : by default it takes as "c"
-->
6. We can define only apex class as controller in lightning component
7. If we want to define more than one apex class in the lightning component how we define
Example 1:
public class Example extends Demo{
}
<!-- <aura:component controller="Example" > -->
Example 2;
<!--
one.cmp
<aura:component controller="Demo" >
</aura:component>
two.cmp
<aura:component extends="c:one" controller="Example">
</aura:component>
-->
8. How to invoke the apex class from client-side controller
Step 1:
var abc =component.get("c.apexMethodName");
a. This will invoke the method from the apex class and run the operation
b. once the server-side operation is completed then it will call callbackaction
Step 2: callback action
abc.setCallback(this,function(response){
logic
});
a. response : what ever the value returened by the apex class this will stored as response
1. getState() : This will return the state of the action
a. SUCCESS
b. ERROR
c. NEW
4. INCOMPLETE
e. ABORTED
f. RUNNING
2. getReturnValue() :
a. if the apex class method has any return type ,value returened by the method
will given by this
Step 3: Enqueue Action
$A.enqueue(abc);
Apex class:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public class Example_One {
@AuraEnabled
public static String callMe(){
return 'Call back from Apex call';
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Component
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
<aura:component controller="Example_One" >
<aura:attribute name="result" type="String" />
<lightning:button label="Submit" onclick="{!c.show}" />
{!v.result}
</aura:component>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Controller
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
({
show : function(component, event, helper) {
// invoke the apex method
var abc =component.get("c.callMe");
// create Callback action : This will return the result
abc.setCallback(this,function(response){
// check the status of the response
var state=response.getState();
// if the response is success
if(state==='SUCCESS'){
console.log('Apex Invoked');
// fetch the value returned by the apex method
var name=response.getReturnValue();
console.log('Result:'+name);
component.set("v.result",name);
}else{
console.log('Method invoke failed');
}
});
//enqueue action
$A.enqueueAction(abc);
}
})
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment