How to use lightning/uiRecordApi In LWC

In this blog let’s see how to use lightning/uiRecordApi for creating records in LWC. By using lightning/uiRecordApi we don’t need any apex call to perform DML operations. lightning/uiRecordApi supports many functions like createRecord, DeleteRecord, Update record, etc. Here let’s understand how to use lightning/uiRecordApi for creating a record in LWC.

Use Case

I have a custom object named Quotes. In the opportunity detail page button whenever a new quote button is clicked, automatically create a quote record. Then navigate to its detail page.

Here custom object quote has a lookup relationship to the opportunity.

Also Read: Schedule Triggered Flow in Salesforce

Before jumping on to the solution let’s see how the lightning/uiRecordApi supports record creation and what is the syntax of it.

How to use createRecord in LWC?


import { createRecord } from 'lightning/uiRecordApi';

import the above module in the JS code to create a record.

createRecord(recordInput: Record)

The createRecord(recordInput) function returns a Promise object that resolves when the record is created.

Now solve the above use case by using lightning/uiRecordApi. Also, let us see the demo of the functionality.


import {LightningElement,api} from 'lwc';
//Import module
import {createRecord} from 'lightning/uiRecordApi';
import {NavigationMixin} from 'lightning/navigation';
import { ShowToastEvent} from "lightning/platformShowToastEvent";
//Import object
import Quote_Object from '@salesforce/schema/Quote__c'; 
//Import Field
import OPPORTUNITY_FIELD from '@salesforce/schema/Quote__c.Opportunity__c'; 

export default class CreateQuoteRecord extends NavigationMixin(LightningElement) {

    @api recordId;

    //Quick Action Call
    @api invoke() {

    createQuoteRecord() {

        const fields = {};
        //Map the data to the fields.
        fields[OPPORTUNITY_FIELD.fieldApiName] = this.recordId;
        //Prepare config object with object and fields.
        const recordInput = {
            apiName: Quote_Object.objectApiName,
        //Invoke createRecord by passing the config object.
            .then((data) => {
                //store record Id.
                this.quoteId =;
                //Navigation to detail page.
                    type: 'standard__recordPage',
                    attributes: {
                        recordId: this.quoteId,
                        objectApiName: 'Quote__c',
                        actionName: 'view'
            .catch((error) => {
                //If error display toast message.
                    new ShowToastEvent({
                        title: "Error creating record",
                        message: error.body.message,
                        variant: "error",

Also Read: How to call flow from Quick Action in Salesforce


<?xml version="1.0" encoding="UTF-8"?>
		<targetConfig targets="lightning__RecordAction">

Next created a new action button and called the above LWC class. Then added this to the page layout.

lwc quick action


In this blog post, we learned how to use lightning/uiRecordApi for creating records in LWC. Also, we will explore different modules of uiRecordApi in the next post.

Related Quick Link

Leave a Comment