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
Scott Janis 20Scott Janis 20 

Automatically create public links when a file is uploaded

I have a process using FLOW File Upload, where the upload button is on the opportunity reocord and so the orginal file references the opportunity. Additionally, a process occurs where a contract record is spun up as a parent of that opportunity and another reference to the same file is created in the 'Content Document Link' object. This all works as expected. 
My question is,how can I proceed to make this link into a public link automatically. I know I can do this using content delivery but is there a way to write to other objects like 'Content Distribution' where the creation of a public link is done automatcally when this file upload process occurs? 
AbhishekAbhishek (Salesforce Developers) 
Scott, I don't think it is possible through other objects.
Lokesh Kumar NLokesh Kumar N
We can create a Content Distribution immediately after creating a Content Document using a trigger on Content Document from the below code
 
ContentDocumentLink cdl = [select contentdocument.id, contentdocument.title, contentdocument.filetype from contentdocumentlink where linkedentityid = '<ParentObjectId>'];
ContentVersion cv = [select id from contentversion where contentdocumentid = :cdl.contentdocument.id];
ContentDistribution cd = new ContentDistribution();
cd.Name = cdl.contentdocument.title;
cd.ContentVersionId = cv.id;
cd.PreferencesAllowViewInBrowser= true;
cd.PreferencesLinkLatestVersion=true;
cd.PreferencesNotifyOnVisit=false;
cd.PreferencesPasswordRequired=false;
cd.PreferencesAllowOriginalDownload= true;
insert cd;