Create records using UIRecordApi in Lightning web components

 Create account without Apex controller in LWC

It's always recommends to use Lightning Data Service(LDS) of lightning-record-form to create any record from UI. But sometimes we don't need to take inputs from user, just we need to create record with some static data, in this use case we can use UIRecordApi to create any object record without calling apex method and without using lightning record form. 
Create-Record-using-UiRecordApi
1. First import createRecord method from uiRecordApi.
    [import {createRecord} from 'lightning/uiRecordApi';]

2. Create an javascript object with object api name and fields with data to insert.
[// we have to create an object of fields to enter data
const fields = {
Name: accountName.value,
};

// It's the final object to send CreateRecord method to create
const recordInput = { apiName: ACCOUNT_OBJECT.objectApiName, fields };]

3. Now pass the recordInput object to createRecord method to create record.
[// Calling createRecord method of uiRecordApi
createRecord(recordInput)]

Let's see with an example:

CreateRecordUsingUiRecordApi.html
[<template>
    <lightning-card title="Create account using Ui record api" icon-name="standard:account">
        <div class="slds-m-around_medium">
            <lightning-input type="string" label="Account Name"></lightning-input><br/>
            <lightning-button name="CreateAccount" label="Create Account" onclick={handleCreateAccount}>
            </lightning-button>
        </div>
    </lightning-card>
</template>]

CreateRecordUsingUiRecordApi.js
[import { LightningElement } from 'lwc';
import {createRecord} from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

export default class CreateRecordUsingUiRecordApi extends LightningElement {

    handleCreateAccount(){
        // get the entered value from UI
        let accountName = this.template.querySelector('lightning-input');
        
        // we have to create an object of fields to enter data
        const fields = {
            Name: accountName.value,
        };
        
        // It's the final object to send CreateRecord method to create
        const recordInput = { apiName: ACCOUNT_OBJECT.objectApiName, fields };
        
        // Calling createRecord method of uiRecordApi
        createRecord(recordInput)
            .then(account =>{
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'New account has been created',
                        variant: 'success',
                    }),
                );
            })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error while creating account',
                        message: error.body.message,
                        variant: 'error',
                    }),
                );
            });
    }
}]

CreateRecordUsingUiRecordApi.js-meta.xml
[<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>48.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
    </targets>
</LightningComponentBundle>]
You can also find the above code in SWDC WORLD.

COMMENTS

Name

Android Apps,1,Apex,7,Apex Methods,1,Batch Apex,2,Chrome Add-on,1,Chrome browser,1,Coding Best Practices,2,CRM Basics,1,cURL,1,Database Query,1,Dataset,1,Dynamic Apex,3,Dynamic SOQL,1,Einstein Analytics,9,Future Apex,1,Git,1,Google Chrome,1,IDs and Urls Hacks,1,JS for LWC,5,Lightning Components,2,lightning Page,1,Lightning Web Components,14,Linux for Windows users,1,LWC,16,LWC-JEST,7,Queueable Apex,1,Redirect page,1,REGEX,1,REST API,1,Salesforce CRM,1,Salesforce Errors,1,Salesforce Handy Commands,1,Salesforce Interview Questions,14,Salesforce Lightning,1,Salesforce New Features,1,Salesforce Tasks,2,Schedule Apex,1,SLDS,4,SVG Image,3,Test Class,1,Triggers,1,uiObjectInfoApi,1,Visualforce Pages,3,Visualforce Pagination,2,VS Code,2,Wave Analytics,7,Winter Release Notes,1,workbench,1,
ltr
item
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog: Create records using UIRecordApi in Lightning web components
Create records using UIRecordApi in Lightning web components
Crate records using UIRecordApi in Lightning web components,Create account without Apex controller in LWC, Create record uirecordapi without ap ex cls
https://1.bp.blogspot.com/-Ojh7GXrnQVo/Xy_qyStIMpI/AAAAAAAAAa8/747crXvde-UrfE82sOtn6LytZLo1NFMogCLcBGAsYHQ/w400-h161/Create-Record-using-UiRecordApi.JPG
https://1.bp.blogspot.com/-Ojh7GXrnQVo/Xy_qyStIMpI/AAAAAAAAAa8/747crXvde-UrfE82sOtn6LytZLo1NFMogCLcBGAsYHQ/s72-w400-c-h161/Create-Record-using-UiRecordApi.JPG
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog
https://www.swdcworld.com/2020/08/crate-records-using-uirecordapi-in.html
https://www.swdcworld.com/
https://www.swdcworld.com/
https://www.swdcworld.com/2020/08/crate-records-using-uirecordapi-in.html
true
5370056087523941001
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy