Files/documents which are attached in record are stored as Salesforce File. Salesforce File is used to share and collaborate on uploaded files, store files privately, manage version updates, and follow files that are important for update.
By default, public sharable link is not generated for any uploaded file. If we have any use case that whenever image file is attached, it should generate public sharable link so that further processing can be done on that file.
We can generate sharable public link for above scenario using trigger. Let us see code to generate public link.
Below trigger ContentVersionExternalLink will be fired when new file is uploaded and saved in any record.
trigger contentVersionExternalLink on ContentVersion (after insert) {
ContentTriggerHandler.createPublicLinkForFile(trigger.new);
}
ContentTriggerHandler class will handle creating link for saved files.
Above class will generate public sharable link when JPG image is uploaded. This condition can be changed based on requirement.
We have to make file visible for others, for this use below code for ContentDocumentLink object trigger. ContentDocumentLink will hold link between a Salesforce CRM Content document or Salesforce file and where it’s shared.
trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
for(ContentDocumentLink l:Trigger.new) {
l.Visibility='AllUsers';
}
}
SOQL to find public link of record
To check public link is created or not for any record’s file, use below SOQL.
SELECT ContentDocumentId, ContentDocument.Title, ContentDocument.CreatedDate, LinkedEntityId FROM ContentDocumentLink where LinkedEntityId='5002v00002uQHzBAAW' //Replace this id with record id
Test Page

Reference:
Related Posts
Download S3 File using AWS Signature Version 4.0
File Related List in Lightning Web Component
Uploading Files to Microsoft One Drive using Apex
Upload Multiple Files in Lightning Component

Dhanik Lal Sahni is a Salesforce Solution Architect, Independent Consultant, and the founder of SalesforceCodex.com. With nearly two decades of IT experience and extensive expertise in Salesforce architecture, he helps startups and enterprises design scalable, secure, and high-performance CRM solutions using Sales Cloud, Service Cloud, Experience Cloud, Data Cloud, Apex, Lightning Web Components (LWC), and enterprise integration patterns.
Through SalesforceCodex, he shares practical tutorials, real-world implementation guides, enterprise architecture best practices, and interview preparation resources for Salesforce Developers, Architects, and Administrators.
Learn more about his Salesforce consulting and freelance services at dhaniksahni.com.
