We want to create ContentVersion related to the account object. We have one field on account which contains the URL of the file(png/jpg/pdf). And by using that URL we want to create ContentVersion. But we are not getting how to get the body of that file to pass the ContentVersion's versiondata parameter. So we have used HTTP callouts. But response is coming null. Below is the code

Http h = new Http(); 
HttpRequest req = new HttpRequest(); 
req.setHeader('Content-Type', 'application/pdf'); 
HttpResponse res  = h.send(req); 
blob retFile = res.getBodyAsBlob();
ContentVersion conVer = new ContentVersion();
conVer.ContentLocation = 'S';
conVer.PathOnClient = Url+'.png';
conVer.IsMajorVersion = true;
conVer.versionData = retFile;
insert conVer;

Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
ContentDocumentLink conDocLink = New ContentDocumentLink();
conDocLink.LinkedEntityId = accid;
conDocLink.ContentDocumentId = conDoc;
conDocLink.shareType = 'V';
insert conDocLink;

Can you please help us to solve this issue.


1 Answer 1


Your Code works well for me (little change on line 12, I remove the '.png')

Be sure you add the file source in "Setup - > Remote Site Settings"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .