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.
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.
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.
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.