Skip to main content
Newcomer
September 4, 2026
Question

Apex Toolkit Document.fromFiles(ContentVersion) - converting Attachment to ContentVersion?

  • September 4, 2026
  • 0 replies
  • 5 views

Greetings - long time lurker, first time poster :)

I’m writing some apex that creates / sends an envelope from an Opportunity, and it’s my understanding that I need to convert an Attachment SObject to a ContentVersion object in order to add it to the envelope.  I’ve created a method that looks like it should work, however when I try to send the envelope I get 

dfsle.ValidationException: The envelope is invalid.
File type is unsupported.

Here’s my code snippet:

List<dfsle.Document> docList = new List<dfsle.Document>{};
ContentVersion cVersion = new ContentVersion();
Attachment attach = [
SELECT Id, Name, Body, ContentType, OwnerId, ParentId
FROM Attachment
WHERE Id =: attachmentId
LIMIT 1
];
if(attach != null){
cVersion.ContentLocation = 'S'; //S-Document is in Salesforce
cVersion.PathOnClient = attach.Name; //File name with extention
cVersion.Origin = 'H'; //C-Content Origin. H-Chatter Origin.
cVersion.OwnerId = attach.OwnerId; //Owner of the file
cVersion.Title = attach.Name; //Name of the file
cVersion.VersionData = attach.Body; //File content
cVersion.FirstPublishLocationId = attach.ParentId;
insert cVersion;
cVersion = [
SELECT Id, ContentDocumentId, ContentLocation, PathOnClient, Origin, OwnerId, Title, VersionData, FirstPublishLocationId
FROM ContentVersion
WHERE Id = :cVersion.Id
];
}
docList.add(dfsle.Document.fromFile(cVersion));
myEnvelope = myEnvelope.withDocuments(docList);

 

Not sure what I’m missing here.

Has anyone ever had success adding Attachment records to envelopes this way?  Is there a better approach?

 

Thanks in advance!