How to Get Public Link for Salesforce File : krati vishnoi

How to Get Public Link for Salesforce File
by: krati vishnoi
blow post content copied from  Forcetalks
click here to view original post

Documents/reports which are appended in the record are put away as Salesforce File. Salesforce File is utilized to share and work together on transferred records, store documents secretly, oversee rendition refreshes, and follow documents that are significant for the update.

As a matter of course, the public sharable connection isn't produced for any transferred record. In the event that we have any utilization case that at whatever point picture record is appended, it should create a public sharable connection so that further handling should be possible on that document.

We can produce a sharable public connection for the above situation utilizing trigger. Allow us to see code to produce a public connection.

dont miss out iconDon't forget to check out: Call Lightning Web Component From a Quick Action in Salesforce

Now, the trigger will get fired whenever a new file gets uploaded and saved against any record.

trigger generatePublicLink on ContentVersion (after insert) {
public class publicLinkClassHandler {
    public static void createPublicLink(List<ContentVersion> lstOfContentVersions){
        ContentDistribution[] dist = new List<ContentDistribution>();
        for(ContentVersion varContentVersion : lstOfContentVersions){
            //if image uploaded then only create public link
                system.debug('varContentVersion.FileExtension.toLowerCase():' + varContentVersion.FileType);                
                insert dist;
    public static ContentDistribution getContentDistribution(Id contentVersionId){
        ContentDistribution newDistribution = new ContentDistribution();
        newDistribution.ContentVersionId = contentVersionId;
        newDistribution.Name = 'External Link';
        newDistribution.PreferencesNotifyOnVisit = false;
        newDistribution.PreferencesAllowViewInBrowser = true;
        system.debug('createdContentDistribution created);
        return newDistribution;

We need to make the record noticeable for other people, for this utilization underneath code for ContentDocumentLink object trigger. ContentDocumentLink will hold connect between a Salesforce CRM Content record or Salesforce document and where it's shared.

trigger contentLinkForPublic on ContentDocumentLink (before insert) {
    for(ContentDocumentLink {

dont miss out iconCheck out another amazing blog by Krati here: All About Custom MetaData Types | The Salesforce Developer Guide

Check whether the link is generated.

SELECT ContentDocumentId, ContentDocument.Title, ContentDocument.CreatedDate, LinkedEntityId FROM ContentDocumentLink where LinkedEntityId= recordId

The post How to Get Public Link for Salesforce File appeared first on Forcetalks.

December 28, 2020 at 11:29PM
Click here for more details...

The original post is available in Forcetalks by krati vishnoi
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.