Richard N
Richard N

Reputation: 925

Visualforce: Display link to a document in the Documents folder.

I am trying to display a link on a custom VF page to a word document which has been uploaded to a folder in the Documents tab.

For some reason this is not working as expected. The link is not pointing to the document. Security is also not an issue. I am not doing something right in the VF I suppose.

Here is my controller code:

public with sharing class osv_portal_HomePageContoller {
public string strDocUrl;
public osv_portal_HomePageContoller()

{

    try
    {
        List<Document> lstDocument = [Select Name from Document where Name = 'SLA-Technology' limit 1];
        string strOrgId = UserInfo.getOrganizationId();
        strDocUrl = '/servlet/servlet.FileDownload?file='+lstDocument[0].Id;

    }
    catch (Exception e) 
    {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error in getDocumentLogoUrl() ' + e.getMessage()));
        System.debug('Error: ' + e.getMessage());
    }
        System.debug('The URL is ' + strDocUrl);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'The URL returned is ' + strDocUrl));   

    }

}

This is my VF code:

<apex:outputLink value="{!strDocUrl}" id="theLink" styleclass="slabutton">
    <span class="delete">Service Level Agreement</span>
 </apex:outputLink>

Thanks.

Upvotes: 0

Views: 5784

Answers (2)

Ram
Ram

Reputation: 1

Try changing

public string strDocUrl;

to

public string strDocUrl{get;set;}

Upvotes: 0

VictorKilo
VictorKilo

Reputation: 1870

I believe your issue is that you're referencing 1stDocument[0].Id but not pulling Id in your query. Also, as a rule, you should not start variables with numbers.

Try changing the above code to this:

public with sharing class osv_portal_HomePageContoller {
public string strDocUrl;
public osv_portal_HomePageContoller()

{

  try
  {
    List<Document> FirstDocument = [SELECT Id,Name FROM Document WHERE Name = 'SLA-Technology' LIMIT 1];
    string strOrgId = UserInfo.getOrganizationId();
    strDocUrl = '/servlet/servlet.FileDownload?file='+FirstDocument[0].Id;

  }
  catch (Exception e) 
  {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error in getDocumentLogoUrl() ' + e.getMessage()));
    System.debug('Error: ' + e.getMessage());
  }
    System.debug('The URL is ' + strDocUrl);
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'The URL returned is ' + strDocUrl));   

  }
}

Upvotes: 2

Related Questions