Step 1: create apex class with the name "ApexController" and copy the below code.
=========================================
public with sharing class ApexController {
@AuraEnabled
public static List<Contact> getContacts() {
List<Contact> contacts =
[SELECT Id, Name, MailingStreet, Phone, Email, LeadSource FROM Contact];
//Add isAccessible() check
return contacts;
}
@AuraEnabled
public static List<Lead> getLeads() {
List<Lead> leads =
[SELECT Id, Name FROM Lead];
//Add isAccessible() check
return leads;
}
@AuraEnabled
public static List<Account> getAccounts() {
List<Account> Accounts =
[SELECT Id, Name FROM Account];
//Add isAccessible() check
return Accounts;
}
@AuraEnabled
public static List<Contact> getContactsbyName(string name) {
string nam=name;
List<Contact> contacts =
[SELECT Id, Name, MailingStreet, Phone, Email, LeadSource FROM Contact where Name=:nam];
//Add isAccessible() check
return contacts;
}
}
Step 2 : create a component with the name "CallApexclass"
==========
<aura:component controller="ApexController">
<lightning:card title="Bring Account data">
<aura:set attribute="actions">
<lightning:button label="Acc Data" onclick="{!c.Accdata}" />
</aura:set>
</lightning:card>
<lightning:card title="Bring Contact data">
<aura:set attribute="actions">
<lightning:button label="Contact Data" onclick="{!c.Condata}" />
</aura:set>
</lightning:card>
<lightning:card title="Bring Lead data">
<aura:set attribute="actions">
<lightning:button label="Lead Data" onclick="{!c.Leaddata}" />
</aura:set>
</lightning:card>
<lightning:card title="Bring Contact data with name">
<lightning:input aura:id="conid" label="Enter Contact Name" />
<aura:set attribute="actions">
<lightning:button label="Contact Data" onclick="{!c.Condataname}" />
</aura:set>
</lightning:card>
</aura:component>
==========================================================
Step 3 :copy following code in the JS controller
==========================================================
({
Condata : function(cmp) {
// Load all contact data
var action = cmp.get("c.getContacts");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log('%%%%%%%%%% Contact'+JSON.stringify(response.getReturnValue()));
}
});
$A.enqueueAction(action);
},
Condataname : function(cmp) {
// Load all contact data
var action = cmp.get("c.getContactsbyName");
action.setParams({name:cmp.find("conid").get("v.value")});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log('%%%%%%%%%% Contact'+JSON.stringify(response.getReturnValue()));
}
});
$A.enqueueAction(action);
},
Leaddata : function(cmp) {
// Load all contact data
var action = cmp.get("c.getLeads");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log('%%%%%%%%%% lead'+JSON.stringify(response.getReturnValue()));
}
});
$A.enqueueAction(action);
},
Accdata : function(cmp) {
// Load all contact data
var action = cmp.get("c.getAccounts");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log('%%%%%%%%%% Account'+JSON.stringify(response.getReturnValue()));
}
});
$A.enqueueAction(action);
}
})
===========
Step 4: create App to test it
<aura:Application>
<c: CallApexclass/>
</aura:Application>
=========================================
public with sharing class ApexController {
@AuraEnabled
public static List<Contact> getContacts() {
List<Contact> contacts =
[SELECT Id, Name, MailingStreet, Phone, Email, LeadSource FROM Contact];
//Add isAccessible() check
return contacts;
}
@AuraEnabled
public static List<Lead> getLeads() {
List<Lead> leads =
[SELECT Id, Name FROM Lead];
//Add isAccessible() check
return leads;
}
@AuraEnabled
public static List<Account> getAccounts() {
List<Account> Accounts =
[SELECT Id, Name FROM Account];
//Add isAccessible() check
return Accounts;
}
@AuraEnabled
public static List<Contact> getContactsbyName(string name) {
string nam=name;
List<Contact> contacts =
[SELECT Id, Name, MailingStreet, Phone, Email, LeadSource FROM Contact where Name=:nam];
//Add isAccessible() check
return contacts;
}
}
Step 2 : create a component with the name "CallApexclass"
==========
<aura:component controller="ApexController">
<lightning:card title="Bring Account data">
<aura:set attribute="actions">
<lightning:button label="Acc Data" onclick="{!c.Accdata}" />
</aura:set>
</lightning:card>
<lightning:card title="Bring Contact data">
<aura:set attribute="actions">
<lightning:button label="Contact Data" onclick="{!c.Condata}" />
</aura:set>
</lightning:card>
<lightning:card title="Bring Lead data">
<aura:set attribute="actions">
<lightning:button label="Lead Data" onclick="{!c.Leaddata}" />
</aura:set>
</lightning:card>
<lightning:card title="Bring Contact data with name">
<lightning:input aura:id="conid" label="Enter Contact Name" />
<aura:set attribute="actions">
<lightning:button label="Contact Data" onclick="{!c.Condataname}" />
</aura:set>
</lightning:card>
</aura:component>
==========================================================
Step 3 :copy following code in the JS controller
==========================================================
({
Condata : function(cmp) {
// Load all contact data
var action = cmp.get("c.getContacts");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log('%%%%%%%%%% Contact'+JSON.stringify(response.getReturnValue()));
}
});
$A.enqueueAction(action);
},
Condataname : function(cmp) {
// Load all contact data
var action = cmp.get("c.getContactsbyName");
action.setParams({name:cmp.find("conid").get("v.value")});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log('%%%%%%%%%% Contact'+JSON.stringify(response.getReturnValue()));
}
});
$A.enqueueAction(action);
},
Leaddata : function(cmp) {
// Load all contact data
var action = cmp.get("c.getLeads");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log('%%%%%%%%%% lead'+JSON.stringify(response.getReturnValue()));
}
});
$A.enqueueAction(action);
},
Accdata : function(cmp) {
// Load all contact data
var action = cmp.get("c.getAccounts");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log('%%%%%%%%%% Account'+JSON.stringify(response.getReturnValue()));
}
});
$A.enqueueAction(action);
}
})
===========
Step 4: create App to test it
<aura:Application>
<c: CallApexclass/>
</aura:Application>
No comments:
Post a Comment