Lightning Web Component – Preview & Download Attachments

by Sudipta Deb | Apr 3, 2023 | Salesforce | 0 comments

Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

Recently in one of my project, I need to develop a custom lightning web component which will display all the attachments linked with that record and provide a preview and download option. In this blog post, I am going to share the code snippet. 

Important - You can and should use the related list in the page layout to display all the attachments. That way you do not require to write any custom code. My requirement is little different in the project where it is already a custom page with lots of lightning web components and they want to display the attachments. So I have to build this custom solution. 

Apex 

To start with, I need to write this below apex class which will fetch the attachments linked with the record. I passed the recordId from the lightning web component to this apex class/method as parameter.

Lightning Web Component

Here is the code for lightning web component

Here in the above code, I am passing the recordId from lightning web component to apex class. Apex class is returning a map containing ContentDocumentId and document title.

In the lightning web component, I am iterating the list of attachments and printing them each in one row. Each row will also have preview and download option.

Screenshot

Github Repo

Conclusion

I hope this code snippet will help you in case you need to write custom lightning web component to preview and download all attachments from a record.

Disclaimer

This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.

0 Comments

Leave a Reply

Written by Sudipta Deb

Enterprise Cloud Architect, Content Creator, 20x Salesforce Certified, 1x Google Cloud Certified, 2x Copado Certified

Related Posts

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

For the last few years, Salesforce administrators, architects, and security teams have been preparing for a major platform change: the retirement of permissions managed directly through Profiles. Salesforce had previously announced plans to move organizations toward a Permission Set–led security model, with enforcement expected to begin around the Spring ’26 timeframe.

read more...

0 Comments

Leave a Reply