Home SalesforceLightning Adding table row dynamically in Lightning Web Component

Adding table row dynamically in Lightning Web Component

by Dhanik Lal Sahni
Dynamic Table Row In LWC SalesforceCodex.

Recently I got requirement from one of our group member, to create dynamic configurable table creation for creating new record in Salesforce objects.

Use Case:

Create a configurable table for adding/deleting new rows in lightning data table. Types of fields will be configurable while adding new rows. On submit save record in multiple Salesforce objects.

Solutions:

There could be two below solutions for this use case

  1. Create a new dynamic lightning web component
  2. Use record-edit-form to create table

This post will solve our use case using solution approach#1.

1. Create a new dynamic lightning web component

We can create one lightning web component which will handle table creation based on configured columns and it’s type. Before creating data table component, let us create field type components to configure these in data table. This field type component will help us in getting record values easily.

Text Type component

This component will create text field component for table. It will only rendered when cell type is text.

Picklist type component

This component will create picklist field component for table. Picklist values will show based on configuration properties.

Picklist values will be populated based on object name and field name which is setup while configuring cell fields. getPicklist is called to get picklist values and it is rendered in lightning-combobox.

AccountController.getPickList: This apex method will be used to get picklist values. See apex code from below attached apex class.

Create table component

Use above mentioned field components to create dynamic table. This table will add delete icon to delete row from table. On submit, record will be saved in multiple object using lightning data service.

As there could be multiple records to save, each rows fields information is fetched using retrieveRecords methods and returned to caller. We have user css class to get each row fields information.

Test Component:

Now we are ready with component creation, let us test above created dynamic component. We have setup three columns, two are text fields and one is picklist. For picklist we have setup object name also. When submit button will be clicked, we need to fetch rows data and we can save using apex code.

const columns=[
    { "label" : "Name", "apiName" : "Name" ,"fieldType":"text","objectName":"Account"}, 
    { "label" : "Phone", "apiName" : "Phone" ,"fieldType":"text","type":"phone","objectName":"Account"},
    { "label" : "Account Source", "apiName" : "AccountSource","fieldType":"picklist","objectName":"Account" }
];

Apex Code:

Test Page:

Related Posts

Dynamic Code Execution using Callable Interface

Dynamic Record Page Creation using FieldSet

Generic DataTable in Lightning Web Component

You may also like

23 comments

Vaibhav June 1, 2021 - 10:09 pm

Hi, I am getting this error..

Unknown public property “objectApiName” of element . This is likely a typo on the corresponding attribute “object-api-name”.

can u help

Reply
Dhanik Lal Sahni June 5, 2021 - 7:12 pm

Hello Vaibhav, Please replace objectAPIName to objectApiName in dynamicTable.js part line#7.

Thank You,
Dhanik

Reply
Rasmi October 7, 2021 - 11:32 pm

I have objectApiName in the datatable.js file. Still I get below error.
Error during LWC component connect phase: [Unknown public property “objectApiName” of element . This is likely a typo on the corresponding attribute “object-api-name”.]

Reply
Dhanik Lal Sahni December 7, 2021 - 12:48 pm

Hey Rashmi,

Please check property name in js. If your property name is objectApiName then it will be added as object-api-name. If it is created as objectAPIName then use like object-a-p-i-name in template attribute. Character Casing impact usage in template. So please check and update code accordingly.

Thank You,
Dhanik

Reply
JONATHAN S FRIEDL July 6, 2021 - 2:01 am

Hello, is there a way to create the rows to support datatype textArea (long)(like this text box I’m currently writitng in) for individual rows, so that it displays more vertically? iI tried using class=”slds-scrollable_y”,.. I dont see it as supported in the documentation, also how would you make a field required?

Thank you for this stuff all very great examples

Reply
Dhanik Lal Sahni July 8, 2021 - 5:12 pm

Hello Jonathan,

Similar to c-input-table-cell, you can create one component for text area like c-input-text-area and then you can use it. in InputTextArea component add text area component. Add template like below in that component

<template>
<lightning-textarea
name=”myText”
label=”Your Name”
message-when-value-missing=”This field is required.”
required>
</lightning-textarea>
</template><

Thank You,
Dhanik

Reply
Chris July 6, 2021 - 10:59 pm

I don’t understand how the columns are being created via the CSS component. Can you elaborate?

Reply
Dhanik Lal Sahni December 7, 2021 - 1:22 pm

Hey Chris,

It is done using dynamicTable.html code line # 17, 18.

Thank You,
Dhanik

Reply
Harini August 31, 2021 - 2:54 am

HI,
The above helped a lot but I am also looking for lookup field to add the dynamic table. I am seeing some issues when I try to code it. Could you please provide some suggestions on how to tackle on javascript where you used field-type,field,record on individual components.

Thank you in advance!

Reply
Dhanik Lal Sahni August 31, 2021 - 4:16 am

Hey Harini,

What issue are you getting, Please share that so that I can help you? Ping me in my telegram channel or linked-in profile to discuss 1-to-1.

Thank You,
Dhanik

Reply
J S Deepthi September 29, 2021 - 6:04 am

Error during LWC component connect phase: [Cannot read properties of undefined (reading ‘0’)]. I am getting this error

Reply
Dhanik Lal Sahni December 7, 2021 - 12:59 pm

Hey Deepthi,

Please check property name in js. If your property name is objectApiName then it will be added as object-api-name. If it is created as objectAPIName then use like object-a-p-i-name in template attribute. Character Casing impact usage in the template. So please check and update the code accordingly.

Thank You,
Dhanik

Reply
Laxmi October 7, 2021 - 10:59 pm

Error during LWC component connect phase: [Cannot read properties of undefined (reading ‘0’)]
I am getting above error.
function – DynamicTable.connectedCallback
Please help

Reply
Dhanik Lal Sahni December 7, 2021 - 12:53 pm

Hello Laxmi,
Looks like you are not passing the columnList from the caller component. This should be passed otherwise you will get this error. Alternatively, you can add code for null and undefined check in connectedCallback method for columnList attribute.

Thank You,
Dhanik

Reply
Morgan Paboeuf October 26, 2021 - 9:34 pm

Hello,
I have to build a table with 5 columns, one of which is a combobox, I have trouble making my rows communicate with each other, for each row I have to retrieve the values of the other columns and those selected in my combobox, how do I go about doing this? can you please help me? I have already spent enough time on this and I am having trouble realizing it

Reply
Dhanik Lal Sahni October 27, 2021 - 10:07 am

Hello Morgan,

In component InputPickListCell raise event with selected value to caller component. Caller component will handle event and based on row selected it should populate values in other fields. In each row, we are storing uuid which will help you in retrieving the selected row.

Thank You,
Dhanik

Reply
Dynamic Record Page Creation using FieldSet | SalesforceCodex November 18, 2021 - 10:11 pm

[…] Adding table row dynamically in Lightning Web Component […]

Reply
vivek dongardive December 28, 2021 - 11:28 am

Hi sir, how to display existing record in account and below that add new row button. i am able to get add row button but not able to get existing record.

Reply
Dhanik Lal Sahni December 29, 2021 - 9:57 am

Hello Vivek,

Have you tried to debug the issue? Is there any error in the console log? Please check and confirm.
We can discuss this on call, if required.

Thank You,
Dhanik

Reply
Raghu November 29, 2022 - 11:37 am

Hi Dhanik,

I am working on a similar requirement to this topic, the ask is to display custom picklist in data and make it editable to inline edit. I would like know if you are available to assist. If you can let me know the best time/way to connect.

Reply
Dhanik Lal Sahni November 29, 2022 - 7:40 pm

Hello Raghu,

Yes, let us connect on linked-in.

Thank You,
Dhanik

Reply
Bhupal January 25, 2023 - 10:35 pm

No MODULE named markup://c:inputTableCell found : [markup://c:dynamicTable]

I am getting this error , I am not able to figure the error.
Please help me .

Reply
Dhanik Lal Sahni February 7, 2023 - 9:12 pm

Hello Bhupal,

Looks like you have not created LWC inputTableCell or given a different name for that. Please check this component is exist in your application. If not, create and try once again.

Thank You,
Dhanik

Reply

Leave a Comment