Skip to main content
dave.alvaor
Newcomer
Newcomer
September 4, 2026
Question

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

  • September 4, 2026
  • 1 reply
  • 27 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!

1 reply

Docusign Employee
September 18, 2026

Hello Team

 

I looked into this issue and double-checked how the DocuSign eSignature Apex SDK handles file conversions.

 Could you please try as below: and test it out

Attachment a = [
SELECT Name, Body, ContentType, ParentId
FROM Attachment
WHERE Id = :attachmentId
LIMIT 1
];

String fileName = a.Name;

// Ensure the filename includes a valid extension if missing
if (!fileName.contains('.')) {
if (a.ContentType == 'application/pdf') {
fileName += '.pdf';
} else {
throw new IllegalArgumentException(
'Attachment must have a supported file extension.'
);
}
}

ContentVersion cv = new ContentVersion(
Title = fileName,
PathOnClient = fileName,
VersionData = a.Body,
ContentLocation = 'S',
FirstPublishLocationId = a.ParentId
);
insert cv;

// Query the inserted ContentVersion to construct the DocuSign document
cv = [
SELECT Id, ContentDocumentId, Title, PathOnClient, VersionData
FROM ContentVersion
WHERE Id = :cv.Id
];

dfsle.Document document = dfsle.Document.fromFile(cv);
myEnvelope = myEnvelope.withDocuments(
new List<dfsle.Document>{ document }
);

Key Takeaways:

  • Validate the File Extension: Ensure PathOnClient matches the actual blob format.

  • Convert Unsupported Formats: Files like .msg, .eml, or .zip aren't natively supported by DocuSign and must be converted to PDF before sending.

  • Ignore Irrelevant Fields: Fields like Origin, OwnerId, or FirstPublishLocationId do not impact DocuSign's file-type validation.

  • Best Practice: For any new implementations, skip legacy Attachment objects entirely and use Salesforce Files (ContentVersion) directly.

Thankyou

Sai