Saving Records

You can take advantage of the built-in create and edit record pages in Salesforce1 to create or edit records via a Lightning component.
The following component contains a button that calls a client-side controller to display the edit record page.
<aura:component>
    <lightning:button label="Edit Record" onclick="{!c.edit}"/>
</aura:component>
The client-side controller fires the force:recordEdit event, which displays the edit record page for a given contact ID. For this event to be handled correctly, the component must be included in Salesforce1.
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.

Saving Records using a Lightning Component

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.

Note

Note

If you create a custom form to handle record updates, you must provide your own field validation.

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}"/>.

In your client-side controller, provide any field validation and pass the record data to a helper function.
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);
}
In your component helper, get an instance of the server-side controller and set a callback. The following example upserts a record on a custom object. Recall that setParams() sets the value of the expense argument on the server-side controller’s saveExpense() method.
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);
}