Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.