Related List with Inline Edit | Lightning Component Salesforce

Suyash N
Suyash N 8.5k Views 81 comments

Hey Users,

Sometimes it is painful if you have to edit multiple child records and you have to open each record and update them. This component will make Related List with Inline Edit support. You can edit multiple rows and save them instantly. Also, if any error will occur, it will be visible instantly as Toast Message.

This component supports multiple salesforce datatypes and all necessary validations are already present. Pros and cons of the component are-

Pros of Component

  • Single Unit Component
  • Major Exceptions Already Handled.
  • Inline Edit based on field permission. If no permission, no Inline Edit.
  • Auto Scroll-bar if more than 5 Child Records are present.
  • Supports major SF datatypes like Text, Textarea, Picklist, Number, Percent, Currency, Date, Time, DateTime, Checkbox, etc.

Cons of Component

  • Lookup, Rich Text Area, Geo-Location are yet in view mode only.

Demo GIF

related list with inline edit lightning salesforce gif

Also Check, Multi-Select Lookup Lightning Component

Related-List-Inline-Edit-Multiple-datatype-supported

Step-1: Create an Apex controller

Our apex controller will have all the necessary Validations. Apex class will fetch the child records and save the records. Here we are using a wrapper class to pass data to the lightning component. Go to Dev. console > File > New > Apex Class.

We need to create an Exception class first to throw the custom exception.

Create MyException.cls Apex class.

Create AdvancedRelatedListController.cls Apex class and paste the below code.

Step-2: Create the AdvancedRelatedList Lightning Component.

For creating a new Lightning Component, go to dev. console > File > New > Lightning Component.

Now open the AdvancedRelatedList.cmp file and paste the below code.

Now open the controller file AdvancedRelatedListController.js and paste the below code.

Open the helper file AdvancedRelatedListHelper.js and paste the below code.

Open the helper file AdvancedRelatedList.design and paste the below code.

Now finally paste the CSS in the AdvancedRelatedList.css file.

Step-3: Add the component on the record page.

Our component is ready to use. Let’s put it on the Record Detail Page. Go to your record > Click on setting Gear on top > Edit Page.

On the Lightning App Builder in the Left Pane, find Advanced Related List and drop it on the record page. Now you need to set the Header, Object API name, Fields API Name List, Icon Name, and check the checkbox if you want the Inline Edit button.

You can check the list of available icons here LDS Icons.

Add Advanced Related List to Lightning Page

Hurray… All Set… Now Save and Activate the Page from the top and click on the back Icon. Your Related List component is visible now.

Advanced Related List with Inline Edit Lightning Salesforce

Also Check:

For any queries or suggestions/queries, comment below.

Cheers … Happy Coding … ūüôā

Share This Article
81 Comments
Subscribe
Notify of
guest
81 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Hamish

Hi, I am getting a compile error when saving the apex class: Invalid type: MyException. Can you please provide some guidance on how to resolve this?

newbe

absolutely great and exactly what i need. however, i have no idea what the test class for the controller should look like. can you help here?

Hamish

Any chance you have the test class ready to go?

Mark

Any chance you’ve converted this to a Lightning Web Component?

Rocky

Thanks for sharing! Test class and eventually an LWC would be awesome! Looking forward to building this out and testing.

Alphy

Hi, do you have the LWC version of the related list yet by any chance? We are running around looking for a solution for a relatedlist with inline editing and lookup fields.

Destiny Russo

Any word on that LWC? Thank you so much this is amazing and exactly what we need in our Community.

Andrew

This is awesome, Is it possible to show more than 5 rows and can you sort a column ?

Andrew

Many thanks for the quick answer !

Andrew

Hi Suyash, this is awesome, but I need a test class to deploy ? Do you have the code for it ?

Andrew

Ok great I really want to deploy soon

John Doe

Anyway to get this to work with AccountContactRelation object? Trying to display any related contact while being able to edit.

Thank you!

Gretchen M. Goulet

Suyash:

I’m wondering if you could quote for me how much you would charge to….

  1. Complete the test Class and
  2. Alter this such that the Name renders as a link to the record like other standard related lists.
Juli

I’ve been looking for something like this for awhile to replace some old Visualforce written to allow inline editing. Any chance you’ve written a test class for this yet that you would be willing to share?

Juli

I wrote one that gives me 88% coverage. It isn’t the prettiest thing but I’m happy to share if someone needs it.

Julianne Boley

Last edited 2 years ago by Julianne Boley
Akshay

Hi Juli,

Can you please share the test class that you have written with me?

Thank you
Akshay

Pavan B

HI

I am trying to add a hyperlink in the data table so it is easy to navigate to the contact record page currently this component displaying all fields as text fields. Any idea on this

Paul Ginsberg

The problem with URL fields is that click on them automatically opens new window, which quickly becomes cumbersome for end users. Fully supported formula fields (or lookups), with hyperlinks working are nicer as you can control whether or not a new window opens.

Akshay

Hi Suyash,

This is working great for me. Thank you for this. It would be great if there was a test class for this. Can you also please provide the test class for this code?

Thank you
Akshay

Lee Gregory

Here is a basic account/opp test class, hopefully this helps someone out there struggling to get it going. Replace with your own specific custom fields if you need to increase code coverage.

Pavan B

Hi Suyash,

Quick Question – in this component default all fields showing editable fields is it possible to change selected fields as non – editable fields on component UI? I made a couple of fields as read-only in profile level still I am able to edit those fields save records from component any thoughts on this.

Thanks,
Pavan

Pavan B

Hi Suyash

Thanks for your quick update. I am trying to add a lock on fields to a specific profile. I have shown it in the attached image is there any way to do this?

Lock.PNG
Garima

Hello I have field dependencies and for the controlling field it is showing all picklist values but for the dependent picklist also it is showing all the values like a normal picklist irrespective of the value selected in the controlling field. Let me know if you have any solution for this.
Thank you!

Christian

Any thoughts on filtering based on recordtype or anything? Also any updates on a working test class?

Bradley Forbes

Is there a way to click the pencil icon and ALL of the records are unlocked to be edited rather than having to do it for each record?

Lee Gregory

Bradley – we accomplished this by adding a new mode called EditAllMode with it’s own pencil in the header column. That calls a new component EditAll function which loops through all the records and sets them to EditMode true. Likewise we added a new SaveAll function that also loops through and saves each record and resets the modes. Pretty easy to add and users are quite happy with the results.

Jeremy

Hey Lee, this would be great! Do you have the code for this? I am new and trying to learn how this would be added to the above.

Lee Gregory

Jeremy, I’ll try to summarize the changes:

Here’s how you add the new EditAllMode in the component:

Also in the component, here are the new edit pencils in the header column, it goes in right after the column header label field iteration:

In the controller, here are the new functions to handle the looping:

And you also need to change the doInit function to handle the resetAll:

I think that is all of the changes, I hope this helps!

Jeremy

Thanks so much Lee! This is exactly what I was looking for. I really appreciate your help!

Claire

Hello Suyash Nolkha
Thanks for this component, it’s very useful.
I need to add a formula image in my related list. Could you tell me how can i solve that?

Claire

Could you tell me how to add the New button?
Thanks

Paul Ginsberg

I’ve implemented it, but the button is elsewhere on the screen which is slightly counterintuitive for users. It would be great if the “New” button was alongside, just like with normal Related List views. I don’t mind if it just pops up a new window.

shezi

can you provide the code for new button

Paul Ginsberg

Thanks hugely for this. My bit of feedback, for people not familiar with Lightning Web Components is:

For “AdvancedRelatedListController.js”
– Go into the Developer console.
– File | Open Resources
– In “Select an item to open” put “AdvancedRelatedList”
– Double click on AdvancedRelatedList.cmp
– Click on Controller (on the right hand side)
– Paste the code and File | Save

The other elements are also on the same screen
– AdvancedRelatedListHelper.js -> Helper
– AdvancedRelatedList.design -> Design
– AdvancedRelatedList.css -> Style

Last edited 1 year ago by Paul Ginsberg
shezi

How we can add a new button to create new related list records.in this example refresh button is available but how we add new records

brandon

I have the component visible but it is not showing the related records. Is there a setting I missed somewhere?

Priya Shrivastava

Hi, I am getting ‘Your entry isn’t a valid increment’ with the decimal number field, Is there any way to fix?

Whit Casey

Hello! I am also getting the the “Your entry isn’t a valid increment” with my decimal number fields. After looking for answers, I have not come across a definitive answer. Has this been solved?

Last edited 1 month ago by Whit Casey
Amy B

Hi, I was able to follow the directions, but after saving the Advanced List to the page layout I receive this error “Attempt to de-reference a null object”

For information, I am trying to load opportunities and not the contact like the example shown.

I also have NO experience with Apex, I am very, very new, so I have no idea where to start with this error.

Lance R.

There’s a chance you don’t have the API name for the fields within the Page setup. I had the same issue, I just assumed the record name on the object was just “Name”, but it turned out to be a different API name. I copied the API name from Object Manager and pasted it directly in, no further issues.

Niamh

This looks great, thanks for sharing!

I am verrrrry new so following this step by step very closely. I am encountering the following errors:

First, when I save a Class, the dev console saves it with a .apxc extension, not .cls — not sure if this is expected or an actual error, but noting here in case it’s relevant.

In Step 2, when I paste the code for the AdvancedRelatedListController.cls & save, I get 85 problems, mostly unexpected tokens, Invalid types, Variable does not exist, & more. Beyond that, I’m getting the following when saving this .js files. Perhaps these exceptions are due to the initial error with the .cls file?

“FIELD_INTEGRITY_EXCEPTION:

Failed to save AdvancedRelatedListController.js: ESLINT_ERROR: {c:AdvancedRelatedList – CONTROLLER} line:col [6:45] –> Parsing error: Unexpected token ; : Source”

“FIELD_INTEGRITY_EXCEPTION:

Failed to save AdvancedRelatedListHelper.js: ESLINT_ERROR: {c:AdvancedRelatedList – HELPER} line:col [22:67] –> Parsing error: Unexpected token ; : Source”

Are there steps I’m missing? Do I need to Find & Replace something in my own text editor or should this be a direct Copy + Paste? As I said, I’m super beginner, so any advice you can give would be hugely appreciated!!

Matty Q

Same issue. I think we are doing something simple wrong. Any luck solving?

Matty Q

I found the issue. The code example above is rendered with escape characters.
Search and replace:
& with &
> with >
&lt; with <

Matty Q

I got this working with the changes, but the lack of lookups is making this not workable. Is there anything I can do to add that? I experimented with swapping out:

with:

but that generated an error related to the fieldwrapper:

Constructor not defined: [LitLink_Setup_Inline_Controller.FieldWrapper].<Constructor>(String, String, String, String, Boolean, List<Schema.SObjectType>)

So I tried defining another wrapper:

but it didn’t like that either.

Matty Q

I also tried adding this, but that did not go anywhere either:

Jennifer Kilman

How can I lock this related list once a record has been submitted for approval?