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
- Create a new dynamic lightning web component
- 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
23 comments
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
Hello Vaibhav, Please replace objectAPIName to objectApiName in dynamicTable.js part line#7.
Thank You,
Dhanik
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”.]
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
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
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
I don’t understand how the columns are being created via the CSS component. Can you elaborate?
Hey Chris,
It is done using dynamicTable.html code line # 17, 18.
Thank You,
Dhanik
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!
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
Error during LWC component connect phase: [Cannot read properties of undefined (reading ‘0’)]. I am getting this error
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
Error during LWC component connect phase: [Cannot read properties of undefined (reading ‘0’)]
I am getting above error.
function – DynamicTable.connectedCallback
Please help
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
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
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
[…] Adding table row dynamically in Lightning Web Component […]
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.
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
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.
Hello Raghu,
Yes, let us connect on linked-in.
Thank You,
Dhanik
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 .
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