+ Start a Discussion
rotfilrotfil 

How to download / expose document stored in Document Tab in visualforce

Like you mighta static resource i.e. {!$Resource.resource_name}

How would you do this with a document stored in the Document Tab object? {!$Document.doc_name}

I would rather not include a direct link with document id - doesn't that change from Sandbox to Production?
Best Answer chosen by rotfil
Lokesh KumarLokesh Kumar
HI rotfil,

As you are trying to get the document form the document object so there is a way to get it like below :
 
<p>Image Bandoneon: <apex:image url="/servlet/servlet.FileDownload—file=01570000000toA8" width="256" height="256"/></p>
Note : replace the ID as per your document ID.
But this is not the best practice to refer the document because the document Id will not be the same across sandboxes. so I would suggest go with the below solution.
 
Visualforce
<apex:commandLink action="{!documentId}" oncomplete="window.location.href='../servlet/servlet.FileDownload?file={!docId}';" value=" Download" >
<apex:param name="documentname" value="Checklist" assignTo="{!documentname}"/>
</apex:commandLink>
 
Apex
public String documentname{ get; set; }
public String docid{ get; set; }
public string getdocId()
{
return docid;
}
public void documentId()
{
documentname=System.currentPageReference().getParameters().get('documentname');
List<Document> lstDocument = [Select Id,Name from Document where Name =:documentname limit 1];
string strOrgId = UserInfo.getOrganizationId();
docid=lstDocument[0].Id;
System.debug('docid:'+docid);

}
 
Good Luck coding.

let me know if this help you !!

Thanks !!

All Answers

Lokesh KumarLokesh Kumar
HI rotfil,

As you are trying to get the document form the document object so there is a way to get it like below :
 
<p>Image Bandoneon: <apex:image url="/servlet/servlet.FileDownload—file=01570000000toA8" width="256" height="256"/></p>
Note : replace the ID as per your document ID.
But this is not the best practice to refer the document because the document Id will not be the same across sandboxes. so I would suggest go with the below solution.
 
Visualforce
<apex:commandLink action="{!documentId}" oncomplete="window.location.href='../servlet/servlet.FileDownload?file={!docId}';" value=" Download" >
<apex:param name="documentname" value="Checklist" assignTo="{!documentname}"/>
</apex:commandLink>
 
Apex
public String documentname{ get; set; }
public String docid{ get; set; }
public string getdocId()
{
return docid;
}
public void documentId()
{
documentname=System.currentPageReference().getParameters().get('documentname');
List<Document> lstDocument = [Select Id,Name from Document where Name =:documentname limit 1];
string strOrgId = UserInfo.getOrganizationId();
docid=lstDocument[0].Id;
System.debug('docid:'+docid);

}
 
Good Luck coding.

let me know if this help you !!

Thanks !!
This was selected as the best answer
rotfilrotfil
Ok Lokesh Kumar many thanks - I shall use the second option there. That will do it, I was just hoping there might be a simplier/more direct way!

Many thanks,
rotfil
hs dsd 19hs dsd 19
I was attempting one assignment here.One phrase file i was saved in our salesforce file list fake documents (https://www.nd-center.com/fake-documents.html).Now i used to be looking to get that word document as it's miles by way of the usage of apex elegance and visible pressure web page.