Skip to main content

Hi Team,

I am working on a Salesforce integration using the DocuSign Apex Toolkit, and I need to achieve the following:

  1. Send multiple documents (a Visualforce page converted to a PDF and a DocuSign template) as part of a single envelope.
  2. After all recipients sign the envelope, combine these documents into a single merged PDF.

Here's a brief outline of my current implementation:

  • I generate a PDF from a Visualforce page and store it in Salesforce.
  • I retrieve a pre-configured DocuSign template using its unique ID.
  • I use the dfsle.Envelope class to attach both documents to an envelope and send it for signing using the dfsle.EnvelopeService.sendEnvelopes method.
  • I retrieve the status of the sent envelopes using dfsle.StatusService.getEnvelopeStatus.

Code Excerpt:

 

Apex Code:

dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(new dfsle.Entity(orderRec.Id)); myEnvelope.withRecipients(new List<dfsle.Recipient> { dfsle.Recipient.fromSource(orderRec.Owner.Name, orderRec.Owner.Email, null, 'Owner', new dfsle.Entity(orderRec.Id)) }); myEnvelope.withDocuments(new List<dfsle.Document> { dfsle.Document.fromFile(documentMap.get(orderRec.Id)), dfsle.Document.fromTemplate( Test.isRunningTest() ? dfsle.UUID.randomUUID() : dfsle.UUID.parse(System.label.Marketplace_Docusign_Id), 'Marketplace Commission Reduction Authorization Document Email' ) }); envelopesToSend.add(myEnvelope);

I’ve successfully sent the documents for signing. However, I want to ensure that the two documents are automatically merged into one single PDF after signing, so it can be saved as a single file in Salesforce.

Hi Ashak,

It sounds like you’re making great progress with your Salesforce integration using the DocuSign Apex Toolkit! To achieve the merging of documents into a single PDF after all recipients have signed, you can use the DocuSign API to combine the documents. Here’s a general approach to help you:

  1. Send the Envelope: You’ve already done this part by attaching the documents and sending the envelope for signing.

  2. Check Envelope Status: Continue using dfsle.StatusService.getEnvelopeStatus to check if the envelope is completed.

  3. Retrieve Completed Documents: Once the envelope is completed, use the DocuSign API to retrieve the signed documents. You can use the dfsle.DocumentService.getDocuments method to get the documents from the completed envelope.

  4. Combine Documents: After retrieving the documents, you can use a PDF manipulation library to merge them into a single PDF. Unfortunately, the DocuSign Apex Toolkit does not provide direct support for merging PDFs, so you’ll need to use an external library or service for this step.

  5. Save the Merged PDF: Finally, save the merged PDF back to Salesforce.

Here’s a more detailed example of how you might implement these steps:

// Step 1: Send the envelope (already done)

// Step 2: Check envelope status
dfsle.EnvelopeStatus envelopeStatus = dfsle.StatusService.getEnvelopeStatus(envelopeId);
if (envelopeStatus.status == 'completed') {
// Step 3: Retrieve completed documents
List<dfsle.Document> signedDocuments = dfsle.DocumentService.getDocuments(envelopeId);

// Step 4: Combine documents (using an external PDF library or service)
// Example using an external service (pseudo-code)
Blob mergedPdf = PDFMergerService.mergeDocuments(signedDocuments);

// Step 5: Save the merged PDF to Salesforce
ContentVersion contentVersion = new ContentVersion();
contentVersion.Title = 'Merged Signed Document';
contentVersion.PathOnClient = 'MergedSignedDocument.pdf';
contentVersion.VersionData = mergedPdf;
insert contentVersion;
}

Thank you so much and if you need any help you can raise a support case anytime at https://support.docusign.com


Hi ​@Mahmoud.Essam,

Thank you for your response.

Yes, we can merge once the documents are completed in Apex but i needed an direct functionality or configuration if docuSign provides such options

but the main constraint was to merge the PDF’s in Salesforce Apex, we can use the external callout to merge pdf’s but unfortunately all the API Callouts are paid versions, I needed an flexibility from docusign it self so that I wont pay to other vendor’s to specific merge functionality.

 


Reply