createRecord(recordInput)

Creates a record.

Syntax 

1import { createRecord } from 'lightning/uiRecordApi';
2createRecord(recordInput: Record): Promise<Record>

User Interface API Resource 

1POST /ui-api/records

createRecord uses this User Interface API resource.

Parameters 

Parameter NameTypeDescriptionRequired?
recordInputObjectA RecordInput object used to create the record.Yes

recordInput takes two properties.

Property NameTypeDescriptionRequired?
apiNameStringThe API name of a supported object.Yes
fieldsObjectMap 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

Returns 

A Promise object that resolves with the created record. The record contains data for the fields in the record layout.

Usage 

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