<aura:component> <lightning:button label="Edit Record" onclick="{!c.edit}"/> </aura:component>
edit : function(component, event, helper) { var editRecordEvent = $A.get("e.force:editRecord"); editRecordEvent.setParams({ "recordId": component.get("v.contact.Id") }); editRecordEvent.fire(); }
Records updated using the force:recordEdit event are persisted by default.
Alternatively, you might have a Lightning component that provides a custom form for users to add a record. To save the new record, wire up a client-side controller to an Apex controller. The following list shows how you can persist a record via a component and Apex controller.
Create an Apex controller to save your updates with the upsert operation. The following example is an Apex controller for upserting record data.
@AuraEnabled public static Expense__c saveExpense(Expense__c expense) { // Perform isUpdateable() check here upsert expense; return expense; }
Call a client-side controller from your component. For example, <lightning:button label="Submit" onclick="{!c.createExpense}"/>.
createExpense : function(component, event, helper) { // Validate form fields // Pass form data to a helper function var newExpense = component.get("v.newExpense"); helper.createExpense(component, newExpense); }
createExpense: function(component, expense) { //Save the expense and update the view this.upsertExpense(component, expense, function(a) { var expenses = component.get("v.expenses"); expenses.push(a.getReturnValue()); component.set("v.expenses", expenses); }); }, upsertExpense : function(component, expense, callback) { var action = component.get("c.saveExpense"); action.setParams({ "expense": expense }); if (callback) { action.setCallback(this, callback); } $A.enqueueAction(action); }