Newer Version Available
Creating a Record
- Call getNewRecord to create an empty record from a record template. You can use this record as the backing store for a form or otherwise have its values set to data intended to be saved.
- Call saveRecord to commit the record. This is described in Saving a Record.
Create an Empty Record from a Record Template
To create an empty record from a record template, you can’t set a recordId on the force:recordData tag. Without a recordId, Lightning Data Service doesn’t load an existing record.
| Attribute Name | Type | Description |
|---|---|---|
| objectApiName | String | The object API name for the new record. |
| recordTypeId | String | The 18 character ID of the record type for the new
record. If not specified, the default record type for the object is used, as defined in the user’s profile. |
| skipCache | Boolean | Whether to load the record template from the server instead of the client-side Lightning Data Service cache. Defaults to false. |
| callback | Function | A function invoked after the empty record is created. This function receives no arguments. |
getNewRecord doesn’t return a result. It simply prepares an empty record and assigns it to the targetRecord attribute.
Creating a Record
The following example illustrates the essentials of creating a record using Lightning Data Service. This example is intended to be added to an account record Lightning page.
1<aura:component implements="flexipage:availableForRecordHome, force:hasRecordId">
2
3 <aura:attribute name="newContact" type="Object"/>
4 <aura:attribute name="simpleNewContact" type="Object"/>
5 <aura:attribute name="newContactError" type="String"/>
6
7 <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
8
9 <force:recordData aura:id="contactRecordCreator"
10 layoutType="FULL"
11 targetRecord="{!v.newContact}"
12 targetFields="{!v.simpleNewContact}"
13 targetError="{!v.newContactError}" />
14
15 <!-- Display the new contact form -->
16 <div class="Create Contact">
17 <lightning:card iconName="action:new_contact" title="Create Contact">
18 <div class="slds-p-horizontal--small">
19 <lightning:input aura:id="contactField" label="First Name" value="{!v.simpleNewContact.FirstName}"/>
20 <lightning:input aura:id="contactField" label="Last Name" value="{!v.simpleNewContact.LastName}"/>
21 <lightning:input aura:id="contactField" label="Title" value="{!v.simpleNewContact.Title}"/>
22 <br/>
23 <lightning:button label="Save Contact" variant="brand" onclick="{!c.handleSaveContact}"/>
24 </div>
25 </lightning:card>
26 </div>
27
28 <!-- Display Lightning Data Service errors -->
29 <aura:if isTrue="{!not(empty(v.newContactError))}">
30 <div class="recordError">
31 {!v.newContactError}</div>
32 </aura:if>
33
34</aura:component>1({
2 doInit: function(component, event, helper) {
3 // Prepare a new record from template
4 component.find("contactRecordCreator").getNewRecord(
5 "Contact", // sObject type (objectApiName)
6 null, // recordTypeId
7 false, // skip cache?
8 $A.getCallback(function() {
9 var rec = component.get("v.newContact");
10 var error = component.get("v.newContactError");
11 if(error || (rec === null)) {
12 console.log("Error initializing record template: " + error);
13 return;
14 }
15 console.log("Record template initialized: " + rec.sobjectType);
16 })
17 );
18 },
19
20 handleSaveContact: function(component, event, helper) {
21 if(helper.validateContactForm(component)) {
22 component.set("v.simpleNewContact.AccountId", component.get("v.recordId"));
23 component.find("contactRecordCreator").saveRecord(function(saveResult) {
24 if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
25 // record is saved successfully
26 var resultsToast = $A.get("e.force:showToast");
27 resultsToast.setParams({
28 "title": "Saved",
29 "message": "The record was saved."
30 });
31 resultsToast.fire();
32
33 } else if (saveResult.state === "INCOMPLETE") {
34 // handle the incomplete state
35 console.log("User is offline, device doesn't support drafts.");
36 } else if (saveResult.state === "ERROR") {
37 // handle the error state
38 console.log('Problem saving contact, error: ' + JSON.stringify(saveResult.error));
39 } else {
40 console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
41 }
42 });
43 }
44 }
45})The handleSaveContact handler is called when the Save Contact button is clicked. It’s a straightforward application of saving the contact, as described in Saving a Record, and then updating the user interface.