Skip to navigation

How to transform PDF form fields

View as Markdown

This topic demonstrates how to use PDF form field transformation within the Apex Toolkit to convert PDF form fields in your Salesforce library documents to Docusign tabs automatically.

Prerequisites

  1. Install the Docusign eSignature for Salesforce managed package.
  2. Create a subdomain for your Salesforce organization by enabling “My Domain.”
  3. Select the Docusign Setup tab and complete the login step.
  4. Upload a PDF containing form fields to a Salesforce library by going to App Launcher > Sales Console > Files and selecting Upload Files.

Required data

Running the code in this how-to requires this data:

Data elementDescription
Contact IDThe ID of the Salesforce Contact that will receive the envelope.
Content Document IDThe ID of a document stored in a Salesforce library.
Documents to be signedYou must provide at least one document to be signed. To use PDF form field transformation, your document needs to be a PDF file with form fields.
Source IDThe ID of the Salesforce Contact from whom the Docusign envelopes will be sent.

Step 1. Define your public class and method

Create one public class and one public method that will contain the code for this how-to. You can skip this step if you are adding this code to an existing class.

Apex
public class TransformPDF {
public static void sendEnvelope() {
}
}

Step 2. Create an empty envelope and recipient

Use the dfsle.EnvelopeService.getEmptyEnvelope method to create a new envelope. Specify the Salesforce Contact ID of the record in the Entity parameter that is to be used as the sender of the envelope. Create a Recipient object for each recipient of the envelope. These recipient objects contain the full set of data on the recipient’s role in the signing process: their role, authentication data, status in the workflow, and contact settings. In this how-to, the envelope will have one recipient: a document signer. Once a recipient is created, use the withRecipients method to add it to the envelope.

The accompanying code demonstrates the workflow for adding recipient data to the envelope.

Apex
Id mySourceId = '0034xxxxxxxxxx4QAE'; // The Salesforce Contact ID of the sender
// Create an empty envelope.
dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
new dfsle.Entity(mySourceId));
// We will use a Salesforce Contact ID as a Recipient here
Contact myContact = [SELECT Id, Name, Email FROM Contact WHERE Id = '0034xxxxxxxxxxcQAG'];
// Use the Recipient.fromSource method to create the Recipient
dfsle.Recipient myRecipient = dfsle.Recipient.fromSource(
myContact.Name, // Recipient name
myContact.Email, // Recipient email
null, //Optional phone number
'Signer 1', //Role Name. Specify the exact role name from template
new dfsle.Entity(myContact.Id));
// Add Recipient to the Envelope
myEnvelope = myEnvelope.withRecipients(new List<dfsle.Recipient> { myRecipient });

Step 3. Get a document with PDF options enabled

Create a new dfsle.Document using the dfsle.DocumentService.getDocuments method. In this how-to, you will use a PDF file containing form fields that you’ve uploaded to a Salesforce library. To get the Content Version ID of that document, write a query using Salesforce Object Query Language (SOQL) to find Content Versions associated with the Content Document ID. Passing that Content Version ID to the getDocuments method will return a list of dfsle.Document objects. Index the returned list to get the first document and use the .withPdfOptions method to enable PDF form field transformation. Then add the document to your envelope.

The accompanying code demonstrates how to get your document from Salesforce and add it to the envelope.

Apex
Id myFileId = [SELECT id from ContentVersion where ContentDocumentId = '0694xxxxxxxxxxyQAD' LIMIT 1].id; // Content version ID of document to send
dfsle.Document myDocument = dfsle.DocumentService.getDocuments(ContentVersion.getSObjectType(), new Set<Id> { myFileId }).get(0);
myDocument.withPdfOptions(new dfsle.Document.PdfOptions(
true, // Whether to transform PDF form fields
1)); // The recipient for which to apply the tags
//Add document to the Envelope
myEnvelope = myEnvelope.withDocuments(new List<dfsle.Document> { myDocument });

Step 4. Send the envelope

Send the envelope using the dfsle.EnvelopeService.sendEnvelope method. The sendNow parameter is set to true to indicate that you want to send the envelope immediately. Setting the sendNow parameter to false will save the envelope as a draft so it can be sent later.

The accompanying code demonstrates how to send the envelope.

Apex
// Send the envelope
myEnvelope = dfsle.EnvelopeService.sendEnvelope(
myEnvelope,
true);

Step 5. Execute your method

To execute your code, go to the Developer Console in Salesforce, open the Debug menu, then select Open Execute Anonymous Window. Type the accompanying code in the window and select Execute.

If you skipped step 1 and added your code to an existing class or method, you’ll need to replace the class and method names with your own.

View the source code for this how-to on GitHub: Apex

Apex
PdfTransform.sendEnvelopeMethod();

Expected response

If your API call is successful, the recipient should receive an email notification asking them to sign the documents in the envelope. All form fields from your PDF will be transformed to Docusign tabs.

Next steps