Hello Everyone, In this post, I will let you know about content document object and it's related objects and also provide apex code to save file in content document object.
Save file in content document object
ContentDocument don't have to create. It get created when you creates it's child object that is ContentVersion.
ContentVersion stores document information similar like attachment object of salesforce.
ContentDocumentLink will share the files with user, records, groups. With this you can attach same file under multiple salesforce records.
Syntax:
//Create Document Parent Record
Account acc = new Account(Name='My Account');
insert acc;
//Create Document
ContentVersion cv = new ContentVersion();
cv.Title = 'Test Document';
cv.PathOnClient = 'TestDocument.pdf';
cv.VersionData = Blob.valueOf('Content of pdf');
cv.IsMajorVersion = true;
insert cv;
//Get Content Document Id
Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
//Create ContentDocumentLink
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = acc.Id;
cdl.ContentDocumentId = conDocId;
cdl.shareType = 'V';
insert cdl;
Sample Code: In the below sample code, I am getting data from a visualforce page(MyPDF) that is in pdf format.
MyPDF.vfp
<apex:page renderAs="PDF" standardController="Account">Sample Code
VF Page in PDF Format
</apex:page>
//Please replace hardcoded id with your org data id
Account acc=[SELECT Id,Name,OwnerId FROM Account WHERE Id='0012800000qHGSI'];
PageReference pref = page.MyPDF;
pref.getParameters().put('id',acc.Id);
pref.setRedirect(true);
Blob bdata = pref.getContent();
//Insert ContentVersion
ContentVersion cv = new ContentVersion();
//S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork.
cv.ContentLocation = 'S';
//File name with extention
cv.PathOnClient = acc.Name+'.pdf';
//C-Content Origin. H-Chatter Origin.
cv.Origin = 'H';
//Owner of the file
cv.OwnerId = acc.OwnerId;
//Name of the file
cv.Title = acc.Name+'.pdf';
//File content
cv.VersionData = bdata;
insert cv;
//get the ContentDocumentId
Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
//Insert ContentDocumentLink
ContentDocumentLink cdl = new ContentDocumentLink();
//Add ContentDocumentId
cdl.ContentDocumentId = conDocId;
//salesforce record Id
cdl.LinkedEntityId = acc.Id;
//V - Viewer permission. C - Collaborator permission. I - Inferred permission.
cdl.ShareType = 'V';
//AllUsers, InternalUsers, SharedUsers
cdl.Visibility = 'InternalUsers';
insert cdl;
Output:
Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.Thank you.
6 Comments
Please Send full code please
ReplyDeletefull code means what?
DeleteHi Manish, could I place this code in an apex class to create an pdf through a flow?
ReplyDeleteYes you can but remember maybe you have to made some changes in code to make it available for the flows.
DeleteHello ,
ReplyDeletewhere is the full code the visualforce page code ?
Can you give me the full code.
Thank you
I have updated the post, please try again to understand and implement this example.
DeleteThank you.
Post a Comment