Skip to content

Instantly share code, notes, and snippets.

@sakthivelsfdc
Last active January 28, 2021 18:54
Show Gist options
  • Save sakthivelsfdc/7a40ccdabf745d6bcbc5ad1ccb5b9159 to your computer and use it in GitHub Desktop.
Save sakthivelsfdc/7a40ccdabf745d6bcbc5ad1ccb5b9159 to your computer and use it in GitHub Desktop.
String yourFilesContent = 'TheBlogReaders.com File upload content';
ContentVersion conVer = new ContentVersion();
conVer.ContentLocation = 'S'; // to use S specify this document is in Salesforce, to use E for external files
conVer.PathOnClient = 'testing.txt'; // The files name, extension is very important here which will help the file in preview.
conVer.Title = 'Testing Files'; // Display name of the files
conVer.VersionData = EncodingUtil.base64Decode(yourFilesContent); // converting your binary string to Blog
insert conVer; //Insert ContentVersion
// First get the Content Document Id from ContentVersion Object
Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
//create ContentDocumentLink record
ContentDocumentLink conDocLink = New ContentDocumentLink();
conDocLink.LinkedEntityId = '0066F00000qNVUv'; // Specify RECORD ID here i.e Any Object ID (Standard Object/Custom Object)
conDocLink.ContentDocumentId = conDoc; //ContentDocumentId Id from ContentVersion
conDocLink.shareType = 'V';
insert conDocLink;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment