Creating a Record
Create a Record using lightning:recordForm
To create a record using lightning:recordForm, leave out the recordId attribute.
This example displays a form that creates an account record with a list of fields. The Cancel and Save buttons are displayed at the bottom of the form.
When the record saves successfully, the fields display pencil icons to denote that inline editing is available. This view is displayed until you refresh or reload the page. Then the form redisplays the record fields without data, ready to create a new record.
Alternatively, use the Full layout type, which loads all fields from the full layout to display a form that creates a record. The columns attribute displays the record fields in two columns that are evenly sized.
Customize Error Handling in lightning:recordForm
When an error is encountered during save, lightning:recordForm displays an error message at the top of the form. You can provide additional error handling using the onerror event handler.
This example displays a toast message when an error is returned.
To return the error message, use event.getParam("message").
To customize the form behavior when a record saves successfully, use the onsuccess event handler.
If you want to provide a custom layout or load custom field values when the form displays, use the lightning:recordEditForm component as shown in the next section.
Create a Record with a Custom Layout Using lightning:recordEditForm
To provide a custom layout for your form fields, use the lightning:recordEditForm component.
Pass in the fields to lightning:inputField, which displays an input control based on the record field type.
This example creates a custom layout using the Grid utility classes in Lightning Design System.
When a server error is encountered,lightning:recordEditForm displays an error message above the form fields. To enable automatic error handling, include the lightning:messages component. Alternatively, provide your own error handling using the onerror event handler.
Another feature that lightning:recordEditForm provides that’s not available with lightning:recordForm is preset custom field values, as shown in the next section.
Prepopulate Field Values
To provide a custom field value when the form displays, use the value attribute on lightning:inputField. If you're providing a record ID, the value returned by the record on load does not override this custom value.
Alternatively, set the field value using this syntax.
If you require more customization when creating a record than what lightning:recordForm and lightning:recordEditForm allow, consider using force:recordData.
Create a Record via a Custom User Interface Using force:recordData
- 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 Editing 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.
The handleSaveContact handler is called when the Save Contact button is clicked. It’s a straightforward application of saving the contact, as described in Editing a Record, and then updating the user interface.