function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ken FernandesKen Fernandes 

New button on Lightning page to download document

Hi all,

I am a business analyst and amateur Salesforce Developer.

I need to add a button a lightning page tied to the 'Contract' standard Salesforce object that needs to do below 2 things at a high level:
1. Read / Preview / Extract a document stored somewhere (can be a network location or in Salesforce itself if possible)
2. Read values of some data-points for e.g. borrower name, etc. tied to said Contract object and insert values tied to said data-points at various locations in this document and spit out final version of document to user, either via email or in some other fashion.

Questions:
1. What is the best control (button) to use for reading or previewing a document?
2. How do I extract values from data-points tied to Contract object after this button is added to that object?
3. How do I insert these values from the Contract object into my document and preview that document all over again?

Any help here would be highly appreciated.

Best,
~Ken
PriyaPriya (Salesforce Developers) 
Hi Ken,

Check the below link that matches your requirement :- 

https://salesforce.stackexchange.com/questions/148568/how-to-download-file-lightning-component

Here they are using URL to download, but you can replace it with lightning button and Controller.js & Apex Controller will remain the same.

Please mark it as best answer if you find the above information helpful.

Regards,
Priya Ranjan 
Ken FernandesKen Fernandes
Hi Priya,

Thank you for the suggestion. It was very helpful in terms of understanding the nuances of getting to the file in Salesforce. However, I am stuck at the below snippet of code:
<a href="" data-id="{!account.acc.ContentDocumentId}" onclick = "{!c.downloadfile}" >Download</a>
Here, I am not sure what the value of 'data-id' attribute should be. Do note that my file download implementation is on the 'Contract' page. I tried the below values with no luck:
  • contract.ContentDocumentId
  • contract.acc.ContentDocumentId
  • contract.con.ContentDocumentId
  • ContentDocumentId
Contract page does not compile if I choose either of the first 3 options and does compile if the 4th option is chose, but null file ID is passed and hence the file is not picked up for download. Do note that I am trying/hoping to access and download a file that is uploaded to this contract and appears in 'Files' related list. 

Questions:
  • What am I doing wrong here? What is the desired correct value of 'data-id' attribute?
  • Is it correct to place the file in 'Files' related list or is there a better location for such a file that I intend to download on click of a button? This file will not change and is a standard template across all contracts.
Any suggestions here are welcome.

Best,
~Ken