The Trailblazer Community in Slack is now in Open Beta—join and connect in real time. [Join us in Slack]
createRecord(recordInput)Creates a record.
1import { createRecord } from 'lightning/uiRecordApi';
2createRecord(recordInput: Record): Promise<Record>1POST /ui-api/recordscreateRecord uses this User Interface API resource.
| Parameter Name | Type | Description | Required? |
|---|---|---|---|
recordInput | Object | A RecordInput object used to create the record. | ![]() |
recordInput takes two properties.
| Property Name | Type | Description | Required? |
|---|---|---|---|
apiName | String | The API name of a supported object. | ![]() |
fields | Object | Map of field names to field values. |
Passing in allowOnSaveDuplicate or recordTypeId isn’t currently supported. To prevent the creation of duplicate records, use a duplicate rule with a matching rule.
Note
A Promise object that resolves with the created record. The record contains data for the fields in the record layout.
Before you use this wire adapter, make sure that there isn’t an easier way to create the data. Consider using the lightning-record-*-form components to work with records. To update a record, use updateRecord instead.
Use createRecord by passing in the record input. To create a RecordInput object, use generateRecordInputForCreate(record,objectInfo). Alternatively, pass in a recordInput object with the apiName and fields properties.
1import { createRecord } from "lightning/uiRecordApi";
2import ACCOUNT_OBJECT from "@salesforce/schema/Account";
3import NAME_FIELD from "@salesforce/schema/Account.Name";
4
5export default class createRecordExample extends LightningElement {
6 name;
7
8 handleInput(event) {
9 this.name = event.target.value;
10 }
11
12 async handleCreate() {
13 const fields = {};
14 // Map the user input to the fields
15 fields[NAME_FIELD.fieldApiName] = this.name;
16
17 // Configure your recordInput object with the object and field API names
18 const recordInput = { apiName: ACCOUNT_OBJECT.objectApiName, fields };
19
20 try {
21 // Invoke createRecord
22 const account = await createRecord(recordInput);
23 } catch (error) {
24 // Handle error
25 }
26 }
27}See Also