lightning web component LDS example

Create Parent-Child Records Using LDS In Lightning Web Component

Lightning Web Component

Hey guys, today in this post we are going to create a sample lightning web component which will asks for a Name and creates an account and its related contact record with that name using lds uiRecordApi.

lightning web component LDS example
lightning web component LDS example output

createRelatedRecord.html [LWC Html Code]

Check Also :  Custom Record Search Functionality In Salesforce Lightning Web Component

createRelatedRecord.js [LWC JavaScript code]

Description : 

To create a record via JavaScript, call the createRecord() method. Our component calls the method in the onclick save handler.

The component’s JavaScript imports createRecord from the lightning/uiRecordApi module. To display notifications for error handling, it imports ShowToastEvent from lightning/platformShowToastEvent. It then imports references to the Account object, Contact object,   Account name field and Contact name field.

Pass the account name to createRecord(recordInput), which creates a new account record based on the name you provide.

And also Pass the contact name and accountId to createRecord(recordInput_Contact), which creates a related contact record based on the name and accountId of account.

Next, provide a handler that creates an account record using createRecord(recordInput). In this example, the handleNameChange(event) method handles a change event from the component HTML file when a user enters an account name.

The createRecord(recordInput) method returns a Promise object that resolves when the record is created. To return record data back to the component, use the then() block. This component returns the account.id and sets it to the accountId property. Since accountId is tracked, when its value changes, the component rerenders.

Handle errors using the catch() block.

createRelatedRecord.js-meta.xml [meta xml file]

Output:

lightning web component LDS example

Related Resources :
Other popular Post :

Follow us for new post updates. & Don’t forget to bookmark this site for your future reference.

Facebook : https://www.facebook.com/sfdcmonkey/

Instagram : https://www.instagram.com/sfdc_monkey/

Buy me a coffeeBuy me a coffee

About The contributor: 

Sheema Dhanraj

1 Post Contribute

sheema dhanraj sfdcmonkey

I am Sheema Dhanraj, Working as a Salesforce.com developer.

 

 

 

 

Post By Sheema:
  1. Create Parent-Child Records Using LDS In Lightning Web Component

(Visited 1 times, 1 visits today)
(Visited 519 times, 1 visits today)

Leave a Reply