Sometimes we can have a requirement to get record Id in onsuccess event of lightning:recordeditform. In below example, I’m creating “Lead” object record using lightning:recordeditform and after successfully saving the record, getting the record Id in onsuccess event.
To create documentation of above component, click DOCUMENTATION in the component sidebar of the Developer Console. Hereaura:description is to show the description of the component and aura:example is to show look and feel of the component.
<aura:documentation>
<aura:description>
<p>An <code>c:Sample</code> component represents an element that executes an action defined by a controller.</p>
</aura:description>
<aura:example name="Sample" ref="c:Sample" label="Using the c:Sample Component">
<p>This example shows a simple setup of <code>Sample</code>.</p>
</aura:example>
</aura:documentation>
The documentation you create will be available at https://.lightning.force.com/auradocs/reference.app, where is the name of your custom Salesforce domain and in the Component Library at https://.lightning.force.com/componentReference/suite.app where is the name of your custom Salesforce domain.
Salesforce has built in component for charts in visualforce page, but the limitation is some types of charts are not available. In such cases, we can use Google Chart for different types of charts. Here is an example of Google Chart in visualforce page.
Find the right chart as per your requirement and go through the documentation for the data requirements of the chart.
You can download the Google Chart Javascript and upload it as a static resource, or you can direct call the Google Chart Javascript url in visualforce page.
Every Google Chart requires data in specific format, create the right forma data in apex controller, before sending it to visualforce page Google Chart.
Example: In below example I’m showing number of Accounts group by Country in a 3D Pie Chart.
Apex Class:
global with sharing class AccountChartController {
@RemoteAction
global static List<AggregateResult> getAccountData(){
List<AggregateResult> accGroupList = [Select BillingCountry Country, Count(Id) NumberOfAccounts
From Account Where BillingCountry != null
Group By BillingCountry];
return accGroupList;
}
}
Visualforce Page:
<apex:page controller="AccountChartController">
<!--Google Chart Javascript Resource-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"/>
<!--Javascript Remote Function To Call Apex Controller Method-->
<script type="text/javascript">
//Load google chart
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);
var accData; //Variable to store data
//Call remote action method
AccountChartController.getAccountData(function(result, event){
accData = result; //get data from apex controller
},{escape:true});
//Draw google chart
function drawChart() {
//Create the data table.
var data = new google.visualization.DataTable();
//Add datatable columns
data.addColumn('string', 'Country');
data.addColumn('number', 'Number of Accounts');
//Add datatable rows
for(i = 0; i< accData.length; i++){
data.addRow([accData[i].Country, accData[i].NumberOfAccounts]);
}
//Set chart options
var options = {
'title':'Accounts Group by Country',
is3D: true,};
//Instantiate and draw the chart.
var chart = new google.visualization.PieChart(document.getElementById('myPieChart'));
chart.draw(data, options);
}
</script>
<!--Identify where the chart should be drawn-->
<div id="myPieChart" style="width: 900px; height: 700px;"/>
</apex:page>
In below example I’m creating Lead records from a Lightning Component. On Save button I’ve implemented the logic to hide the button after click.
Apex Controller:
public class SampleAuraController {
@AuraEnabled
Public static void createLead(Lead objLead){
try{
//Insert Lead Record
insert objLead;
}catch(Exception e){
//get exception message
throw new AuraHandledException(e.getMessage());
}
finally {
}
}
}
Lightning Data Service (LDS) is to serve as the data layer for Lightning. It is similar like Standard Controllers in Visualforce Page.
Advantages of Lightning Data Service:
Lightning Data Service allows to load, create, edit, or delete a record without requiring Apex code and SOQL query.
Lightning Data Service handles sharing rules and field-level security.
Records loaded in Lightning Data Service are cashed and shared across all components.
When one component updates a record, the other component using it are notified.
Lightning Data Service supports offline in Salesforce1.
In below example I’m creating Lead object record using Lightning Data Service. By using the new base lightning component lightning:recordEditForm, we can create and edit records, without custom Javascript and Apex code.
({
handleOnSuccess : function(component, event, helper) {
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Success!",
"message": "The record has been created successfully.",
"type": "success",
"mode": "pester"
});
toastEvent.fire();
$A.get("e.force:refreshView").fire();
}
})
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject