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.
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();
}
})
We can use lightning:dualListbox component to show multi select picklist field values. lightning:dualListbox component represents two side-by-side list boxes. Select one or more options in the list on the left. Move selected options to the list on the right. The order of the selected options is maintained and we can reorder options.
Here in below example I’m using a Custom object “Book”, and the multi select picklist field “Genre” of “Book” object. Apex Class:
public class SampleAuraController {
@AuraEnabled
public static List <String> getPiklistValues() {
List<String> plValues = new List<String>();
//Get the object type from object name. Here I've used custom object Book.
Schema.SObjectType objType = Schema.getGlobalDescribe().get('Book__c');
//Describe the sObject using its object type.
Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
//Get the specific field information from field name. Here I've used custom field Genre__c of Book object.
Schema.DescribeFieldResult objFieldInfo = objDescribe.fields.getMap().get('Genre__c').getDescribe();
//Get the picklist field values.
List<Schema.PicklistEntry> picklistvalues = objFieldInfo.getPicklistValues();
//Add the picklist values to list.
for(Schema.PicklistEntry plv: picklistvalues) {
plValues.add(plv.getValue());
}
plValues.sort();
return plValues;
}
}