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
Brian CBrian C 

Transitioning from Attachments to Files

I am working on transitioning my org's workflow from using Attachments to Files in order to be compatibile with Lightning. I have a specific use case I need guidance on:

In Classic, we have a work process in which an artist uploads an image file to a custom object record. This file is automatically renamed via an Apex controller to a standardized filename. We have a separate Visualforce page using the custom object as its standard controller (also using a custom Apex extension). The Visualforce page renders the image for printing (along with other related data from the record) by referencing the generic file name. 

For Lightning, I have built a Lightning component for file upload. This component uploads the image as a File/ ContentDocument associated with the custom object. How can I replicate similar functionality for the Visualforce page with Salesforce Files? My initial thought was to add a field to the custom object to store a URL for viewing the File, but I'm not sure how to generate such a URL. 
Brian CBrian C
I figured it out. I used the "onuploadfinished" attribute of lightning:fileupload to fire a controller function passing the uploaded file id and custom object id to an Apex controller. That controller then queries for a ContentVersion with a ContentDocumentId matching the uploaded file Id.

Using the ContentVersion.Id and ContentVersion.ContentBodyId, I created a url pointing to  baseUrl + '/sfc/servlet.shepherd/version/renditionDownload?rendition=ORIGINAL_Jpg&versionId='+ContentVersion.Id+'&contentId='+ContentVersion.contentbodyid'. I then wrote that to a URL field on the custom object. 

Seems to be working well.