Home Salesforce Generate Public Link for Salesforce file

Generate Public Link for Salesforce file

by Dhanik Lal Sahni
generatepubliclink_salesforcecodex

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:

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm

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

You may also like

Leave a Comment