Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    • Salesforce Spring ’25 Release: Top Flow Enhancements You Need to Know
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Thursday, May 8
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»File Related List in Lightning Web Component

    File Related List in Lightning Web Component

    Dhanik Lal SahniBy Dhanik Lal SahniNovember 20, 2020Updated:September 10, 20244 Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    File Related List in Lightning Web Component
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Files is the excellent and most robust way to manage and share files in Salesforce. Using files we can

    • Upload any file
    • Search Salesforce for Files
    • Store files up to 10MB (2GB if uploaded through Chatter)
    • Preview uploaded files
    • Follow uploaded files
    • Share with other group or user
    • Share with external customers

    Recently one of our telegram group member asked how we can create file related list in lightning web component. That component can be added in lightning web component when some customize page needed it.

    As we need to show file detail in lightning data table, so we have to customize lightning data table component to show file preview when mouse is over table cell. To create a file related list in lightning web component, we need below steps.

    1. Get list of all files for current records
    2. Create Preview Component
    3. Create Preview table cell component
    4. Customize lightning-datatable component
    5. Create generic dialog component to upload new file
    6. Create List component to show files

    Let us go each step one by one so that we will get complete detail of component creation.

    1 . Get list of all files for current records

    We have to first get all files of current record. This component can be used in any object record. It should get records based on that record id. We will use ContentDocumentLink object to show preview and ContentVersion to show different versions of selected file.

    Wrapper class is created to share only required detail to lightning web component. Wrapper class is extended based on required detail.

    2. Create Preview Component

    Let us create lightning component which will show preview image of file. For this use case we need image element and lightning-card component. Download no preview image from here and upload as static resource nopreview.

    3. Create Preview Table Cell Component

    Now let us create datatable cell component to preview file content. This component will show anchor list to show hyperlink and when mouse is moved over link it will show preview box.

    4. Customize lightning-datatable component

    We have to customize lightning-datatable component so that it will show anchor link and file preview. We have to use component which is created in step3.

    ComponerStructure_SalesforceCodex

    This is file structure for this component. preview.html is part of this component itself.

    5. Create generic dialog component to upload new file

    create a generic dialog box component. This component can be used to show content as dialog or confirmation box.

    We have used slot in this component so that we can also add custom html code in that dialog component.

    6. Create List component to show files

    Now we are ready to merge those component in one component to show files list and upload new file/file version.

    1. File list is created using getFilesList js function.
    2. For new file upload, dialog component is used. File upload elements are added as slot in dialog component at runtime. handleUpload js method is used to handle this use case.
    3. New version of file can be uploaded and stored using Lightning data service. handleUpload js method is used to handle this use case.
    4. Deletion of file is also allowed based on confirmation. This functionality is also created using Lighting data service. deleteFiles js function will handle deletion of file.

    Test Page:

    file related list in Salesforce

    References:

    1. https://salesforcecodex.com/2020/04/extend-lightning-datatable-with-file-upload-element-in-lightning-web-component/
    2. https://salesforcecodex.com/2019/10/show-image-in-lightning-web-component-data-table/
    3. https://help.salesforce.com/articleView?id=collab_files_attach_records.htm&type=5

    ContentDocumentLink contentversion dialog component file related list file upload file version in lightning component filelist lighting-dialog lightning component lightning data table Lightning web component lightning-datatable lwc salesforce slot in lwc
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleDifference between Salesforce WSDL Files
    Next Article UML Sequence Diagram for Salesforce Integration
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Basic Queueable Chaining in Salesforce Apex

    March 31, 2025
    View 4 Comments

    4 Comments

    1. Varun Raghavan on May 11, 2021 6:34 pm

      This is great Dhanik – one thing I think you missed was the NOPREVIEWIMGURL on the filepreview.html, it was giving a bug when the preview was not avaialble.
      just imported the static resource on that component.
      import NOPREVIEWIMGURL from ‘@salesforce/resourceUrl/nopreview’;

      Also ended up tweaking the Apex code to pull an Accounts related objects files as it does in Classic as that was one of my requirements, but this code worked great. Thanks

      Reply
      • Dhanik Lal Sahni on July 3, 2021 1:59 pm

        Thank You Varun. Code is updated for NOPREVIEWIMGURL import.

        Regards
        Dhanik

        Reply
    2. Ruksaar on July 20, 2021 8:27 pm

      Hello
      Does it include sorting and the other standard salesforce functionalitieslike download, share file?

      Otherwise its a great code. thank you

      Reply
      • Dhanik Lal Sahni on July 21, 2021 8:22 pm

        Thank You Ruksaar,

        You can add those required functionalities.

        Regards,
        Dhanik

        Reply
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) custom metadata types (5) design principle (9) file upload (3) flow (14) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (139) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce GraphQL API (3) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) shopify api (3) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) custom metadata types (5) design principle (9) file upload (3) flow (14) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (139) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce GraphQL API (3) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) shopify api (3) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.