21 - createRecord a standard method is LWC javascript

 is createRecord a standard method is LWC javascript ?

ChatGPT said:
ChatGPT

Yes, createRecord is a standard method provided by the lightning/uiRecordApi module in Lightning Web Components (LWC). It allows you to create a new record in Salesforce from JavaScript code in your LWC component.

Here's how you can use createRecord:

  1. Import createRecord: Import it from the lightning/uiRecordApi module.
  2. Specify Object API Name and Fields: Provide the object API name and the fields with values you want to set.

Example of using createRecord

Here’s a simple example that creates a new Contact record:



HTML

<template> <lightning-card title="Account Manager"> <lightning-layout> <lightning-layout-item size="6" padding="around-small"> <lightning-card title="Create A New Account"> <lightning-input name="Account Name" label="Name" onchange={accountNameChangeHandler} ></lightning-input> <lightning-input name="Account Phone" type="phone" label="Phone" onchange={accountPhoneChangeHandler} ></lightning-input> <lightning-input name="Account Website" type="url" label="Website" onchange={accountWebsiteChangeHandler} ></lightning-input> <lightning-button label="Create Account" onclick={createAccount} variant="brand" ></lightning-button> </lightning-card> </lightning-layout-item> <lightning-layout-item size="6" padding="around-small"> <lightning-card title="View Account Record"> <p><lightning-formatted-text value={retAccountName} ></lightning-formatted-text></p> <p><lightning-formatted-phone value={retAccountPhone} ></lightning-formatted-phone></p> <p><lightning-formatted-url value={retAccountWebsite} ></lightning-formatted-url></p> </lightning-card> </lightning-layout-item> </lightning-layout> </lightning-card> </template>

javascript


import { LightningElement,track, wire } from 'lwc'; import { createRecord, getRecord } from "lightning/uiRecordApi"; const fieldArray= ['Account.Name', 'Account.Phone', 'Account.Website']; export default class AccountManagerLDS extends LightningElement { @track accountName; @track accountPhone; @track accountWebsite; @track recordId; @wire(getRecord, {recordId: '$recordId', fields: fieldArray}) accountRecord; accountNameChangeHandler(event){ this.accountName = event.target.value; } accountPhoneChangeHandler(event){ this.accountPhone = event.target.value; } accountWebsiteChangeHandler(event){ this.accountWebsite = event.target.value; } createAccount(){ const fields = {'Name' : this.accountName, 'Phone' : this.accountPhone, 'Website': this.accountWebsite}; const recordInput = {apiName : 'Account', fields}; createRecord(recordInput).then(response => { console.log('Account has been created : ', response.id); this.recordId = response.id; }).catch(error =>{ console.error('Error in creating account : ', error.body.message); }); } get retAccountName(){ if(this.accountRecord.data){ return this.accountRecord.data.fields.Name.value; } return undefined; } get retAccountPhone(){ if(this.accountRecord.data){ return this.accountRecord.data.fields.Phone.value; } return undefined; } get retAccountWebsite(){ if(this.accountRecord.data){ return this.accountRecord.data.fields.Website.value; } return undefined; } }

 

In this example:
  • createRecord is used to create a new record by passing an object with the apiName (e.g., Contact) and fields you want to set.
  • If successful, it returns the new record's ID, which you can then use for other purposes, such as showing a toast notification.
  • If there’s an error, it’s caught and handled in the catch block.

Comments

Popular posts from this blog

API Names in Salesforce

18 - LWC - BEST PRACTICES - For accessing HTML elements and their values in JavaScript

6 - Object Relationships (Lookup and Master-Detail and Junction Objects)