Salesforce File

How to Get Public Link for Salesforce File

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) {
    publicLinkClassHandler.createPublicLink(trigger.new);
}
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
            if(varContentVersion.FileType.toLowerCase()=='jpg'){
                system.debug('varContentVersion.FileExtension.toLowerCase():' + varContentVersion.FileType);                
                dist.add(getContentDistribution(varContentVersion.Id));
            }
        }
        if(!dist.isEmpty())
        {
        	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;
        newDistribution.PreferencesAllowOriginalDownload=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 link:trigger.new) {
        link.visibility='AllUsers';
    }
}

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

Responses

Popular Salesforce Blogs