Create Parent-Child Records Using LDS In 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 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]


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 :

Instagram :

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 developer.





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

(Visited 10 times, 1 visits today)

(Visited 5,887 times, 1 visits today)

3 thoughts on “Create Parent-Child Records Using LDS In Lightning Web Component”

  1. The steps you describe in this article are really good at creating parent-child records using LDS In the lightning web component. Thanks a lot for providing these steps.

  2. Thanks for sharing this. I have one question here. If we more fields in the form. Let say for the example, I have around 20-30 fields  in the form. Do I need to import all the 20-30 fields?


Leave a Comment