0

Im creating the instance of the ContentDistribution for each Contenversion ids. and I'm referring this ContentDistribution's ID in VF page as a URl but its not working. So what I want is when a user generates the PDF of a Case Record in that PDF I should provide all the attached Files/attachment link so that when the user clicks on it. it should navigate him to that Particular Attached file --- The Apex Controller

public class CaseInformationForPDF {
    
    ApexPages.StandardController standardController;
    Public Case caseRecord{get;set;}
    public string recordID ;
    Public List<ContentVersion> contentVersionRecords{get;set;}
    public string pdfView {get;set;}
    Public  list<ContentDistribution> contentDistributionRecords {get;set;} 
    Public list<ContentDistribution> contentDistributionQueriedRecords {get;set;} 
    Public list<ContentDocumentLink> contentVersionRecordsDocumentLink {get;set;}  
    Public Set<id> contentDocumentIds {get;set;}
    
    public CaseInformationForPDF ( ApexPages.StandardController standardController ) {
        
        Try{
            
            this.standardController = standardController;
            recordID = standardController.getId();
            contentVersionRecordsDocumentLink =[SELECT id,ContentDocumentId FROM ContentDocumentLink WHERE  LinkedEntityId =:recordID];
            contentDocumentIds = New Set<id>();
            for(ContentDocumentLink contentDistributionRecord : contentVersionRecordsDocumentLink)
            {
                contentDocumentIds.add(contentDistributionRecord.ContentDocumentId);
            }
            
            contentVersionRecords =[SELECT id,Title,ContentDocumentId,VersionData   FROM ContentVersion WHERE islatest=true and ContentDocumentId in :contentDocumentIds];
            
            contentDistributionRecords = New list<ContentDistribution>();
            
            If(contentVersionRecords.size()>0){
                
                for(ContentVersion cont : contentVersionRecords){
                    ContentDistribution contentDistributionRecord = new ContentDistribution();
                    contentDistributionRecord.name = cont.Title;
                    contentDistributionRecord.ContentVersionId = cont.id;
                    contentDistributionRecord.PreferencesAllowOriginalDownload = true;
                    contentDistributionRecord.PreferencesAllowPDFDownload = true;
                    contentDistributionRecord.PreferencesAllowViewInBrowser = true;
                   
                    contentDistributionRecords.add(contentDistributionRecord); 
                 }
            }   
        }
        catch(exception e)
        {
            system.debug('message -- '+e.getMessage()+ 'line. -  '+e.getLineNumber()+'cause. ' + e.getCause());
        }
    }
    
    public void contentDistributionMethod() 
    {
        contentDistributionQueriedRecords = New list<ContentDistribution>();
        IF(contentDistributionRecords.size()>0)
        {   
            Insert contentDistributionRecords; 
            contentDistributionQueriedRecords = [SELECT DistributionPublicUrl,name,Id FROM ContentDistribution WHERE ID in :contentDistributionRecords];
        }
    }

Visual force page

<apex:page showHeader="true" standardController="Case" extensions="CaseInformationForPDF" standardStylesheets="true" renderAs="PDF" action="{!contentDistributionMethod}">  
    <apex:form >
      <body>
            
            <ul>
                <apex:repeat value="{!contentDistributionQueriedRecords}" var="attachment">
                    aaaa  <li>  <apex:outputLink value="/{!attachment.DistributionPublicUrl}" id="theLink">{!attachment.name}</apex:outputLink></li>
                </apex:repeat> 
              
            </ul>

Can anyone guide me on this?

1 Answer 1

1

Don't include the leading /. The URL is already a full, canonical URL.

3
  • THANK YOU SO SO MUCH. I LITERALLY BANGING MY HEAD FROM PAST3 DAYS. Commented Oct 2, 2022 at 19:34
  • @pavankumar that is how programming goes sometimes. Just need a second pair of eyes. Glad I was able to help.
    – sfdcfox
    Commented Oct 2, 2022 at 20:12
  • @sfdxfox that is true, I was so dumb that I couldn't even noticed and finally you were able to catch it :) Commented Oct 3, 2022 at 9:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .