# `createRecord(recordInput)`

Creates a record.

## Syntax

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

## User Interface API Resource

```html
POST /ui-api/records
```

`createRecord` uses this [User Interface API resource](https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_requests_record_input.htm).

## Parameters

| Parameter Name | Type   | Description                                                                                                                                            | Required?                                                                         |
| -------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------- |
| `recordInput`  | Object | A [RecordInput](https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_requests_record_input.htm) object used to create the record. | ![Yes](https://a.sfdcstatic.com/developer-website/sfdocs/lwc/media/checkmark.png) |

`recordInput` takes two properties.

| Property Name | Type   | Description                                                                                                                    | Required?                                                                         |
| ------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------- |
| `apiName`     | String | The API name of a [supported object](https://developer.salesforce.com/docs/platform/lwc/guide/reference-supported-objects.md). | ![Yes](https://a.sfdcstatic.com/developer-website/sfdocs/lwc/media/checkmark.png) |
| `fields`      | Object | Map of field names to field values.                                                                                            |                                                                                   |

:::note
Passing in `allowOnSaveDuplicate` or `recordTypeId` isn't currently supported. To prevent the creation of duplicate records, use a [duplicate rule with a matching rule](https://help.salesforce.com/s/articleView?id=sales.duplicate_prevention_map_of_tasks.htm).
:::

## 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](/docs/platform/lwc/guide/data-get-user-input.md) to work with records. To update a record, use [`updateRecord`](/docs/platform/lwc/guide/reference-update-record.md) instead.

Use `createRecord` by passing in the record input. To create a RecordInput object, use [`generateRecordInputForCreate(record,objectInfo)`](/docs/platform/lwc/guide/reference-generate-record-input-create.md). Alternatively, pass in a `recordInput` object with the `apiName` and `fields` properties.

```javascript
import { createRecord } from "lightning/uiRecordApi";
import ACCOUNT_OBJECT from "@salesforce/schema/Account";
import NAME_FIELD from "@salesforce/schema/Account.Name";

export default class createRecordExample extends LightningElement {
  name;

  handleInput(event) {
    this.name = event.target.value;
  }

  async handleCreate() {
    const fields = {};
    // Map the user input to the fields
    fields[NAME_FIELD.fieldApiName] = this.name;

    // Configure your recordInput object with the object and field API names
    const recordInput = { apiName: ACCOUNT_OBJECT.objectApiName, fields };

    try {
      // Invoke createRecord
      const account = await createRecord(recordInput);
    } catch (error) {
      // Handle error
    }
  }
}
```

**See Also**

- [Build Custom UI to Create and Edit Records](/docs/platform/lwc/guide/data-salesforce-write.md)
- [Toast Notifications](/docs/platform/lwc/guide/use-toast.md)
