Skip to navigation

How to send an envelope with tabs

View as Markdown

This topic demonstrates how to add tabs to your envelope.

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.

Required data

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

Data elementDescription
Content Document IDThe ID of a document stored in a Salesforce library.
Source IDThe ID of the Salesforce Contact from whom the Docusign envelopes will be sent.

Step 1. Define your class and method

Create the class and method that will contain the code in this example. You can skip this step if you are adding this code to an existing class.

Apex
public with sharing class SendEnvelopeWithTabsController {
@AuraEnabled
public static void sendEnvelopeWithTabs() {
}
}

Step 2. Create your envelope definition

Use the dfsle.EnvelopeService.getEmptyEnvelope method to create an empty envelope definition that will be subsequently completed, created, and sent. Specify the Salesforce source ID of the record in the dfsle.Entity parameter that is to be used as the source object for the envelope.

Apex
Id mySourceId = '00XXXXXXXXXXXXXXAM'; // The ID of the initiating Salesforce object
// Create an empty envelope
dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
new dfsle.Entity(mySourceId));
// The initiating Salesforce entity

Step 3. Specify a document for the envelope

Create a new dfsle.Document and add it to the envelope. This example uses the dfsle.DocumentService.getDocuments method, passing in the ContentDocumentId of a document already stored in a Salesforce library. Then, it uses the Envelope’s dfsle.Envelope.withDocuments method to add the document to the envelope.

Apex
Id myDocumentId = '06XXXXXXXXXXXXXXAQ'; // The ID of a document stored in a Salesforce library
Id myFileId = [SELECT id from ContentVersion where ContentDocumentId = :myDocumentId LIMIT 1].id;
// Add document to the envelope
myEnvelope = myEnvelope.withDocuments(dfsle.DocumentService.getDocuments(ContentVersion.getSObjectType(), new Set<Id> { myFileId }));

Step 4. Define the recipient tabs

Create one tab each of types Sign Here, Initial Here, Date Signed, and Text, and associate them with your document. These are just four of the many tab types supported by the Apex Toolkit. The Sign Here tab is placed in the document using Auto-place (anchor tagging).

Apex
dfsle.Tab myInitialHereTab = new dfsle.InitialHereTab()
.withRequired(true) // Signing optional
.withPosition(new dfsle.Tab.Position(
1, // The document to use
1, // Page number on the document
320, // X position
290, // Y position
null, // Default width
null)); // Default height
// Create a Text tab
dfsle.Tab myTextTab = new dfsle.TextTab()
.withRequired(true) // Signer must enter value
.withPosition(new dfsle.Tab.Position(
1, // The document to use
1, // Page number on the document
210, // X position
305, // Y position
100, // 100 pixels wide
null)); // Default height
// Create a Sign Here tab
dfsle.Tab mySignHereTab = new dfsle.SignHereTab()
.withScale(1.5) // 1.5 scale
.withRequired(true) // Signing mandatory
.withAnchor(new dfsle.Tab.Anchor(
'signed', // Anchor string
false, // Do not allow white space in anchor string
false, // Anchor string is not case sensitive
'right', // Horizontal alignment in relation to the anchor text
true, // Ignore if the anchor text is not present in the document
true, // Must match the value of the anchor string in its entirety
'pixels', // Unit of the x and y offset properties
20, // X offset
0)); // Y offset
// Create a Date Signed tab
dfsle.Tab myDateSignedTab = new dfsle.DateSignedTab()
.withFormatting(new dfsle.Tab.Formatting(
'Helvetica', // Font
'default', // Font color
14, // 14pt size,
true, // Bold
false, // Italic
false, // Underline
false, // Conceal value
false)) // Fixed width
.withPosition(new dfsle.Tab.Position(
1, // The document to use
1, // Page number on the document
330, // X position
470, // Y position
null, // Default width
null)); // Default height

Step 5. Specify the recipient for the envelope

Create a dfsle.Recipient object for each recipient of the envelope and add tabs using the withTabs method. In this example, the envelope will have one signer recipient. Once a recipient is created, use the withRecipients method to add it to the envelope.

Apex
dfsle.Recipient myRecipient = dfsle.Recipient.fromSource(
'Lily TheGSD', // Signer name
'lilythegsd@gmail.com', // Signer email
null, // Signer phone number
'Signer 1', // Signer role
null) // No Salesforce association
.withTabs(new List<dfsle.Tab> { // Associate the tabs with this recipient
myInitialHereTab,
myTextTab,
mySignHereTab,
myDateSignedTab
});
// Add recipient to the envelope
myEnvelope = myEnvelope.withRecipients(new List<dfsle.Recipient> { myRecipient });

Step 6. 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. The Apex Toolkit does not support sending a draft envelope. To send a draft envelope, you will need to call the eSignature REST API outside of the Apex Toolkit.

Apex
myEnvelope = dfsle.EnvelopeService.sendEnvelope(
myEnvelope, // The envelope to send
true); // Send now?

Step 7. Get the status

Docusign administrators can track the status of Docusign envelopes after sending them to recipients by using the Apex Toolkit dfsle.StatusService.getStatus method. This method returns the current status of the envelope (Created, Deleted, Sent, Delivered, Signed, Completed, or Declined), the time and date of the most recent action on the envelope, and other information as a list of dfsle.Envelope.Status objects. This method is passed a set of Salesforce source records that are associated with envelopes and an integer specifying the maximum number of records that will be returned.

Getting envelope status is useful to ensure that the envelope operations you’ve already performed, such as sending, confirming delivery, signing, or completion, have been executed successfully and to detect new status changes for an envelope. You must be the sender of the envelopes to call the dfsle.StatusService.getStatus method.

Apex
Id mySourceId; // The Salesforce ID of the object from which DocuSign envelopes were sent
// Retrieve the five most recent envelope statuses from the Salesforce source object
// The status results are sorted oldest to newest
List<dfsle.Envelope.Status> myStatus = dfsle.StatusService.getStatus(
new Set<Id> { // IDs of the Salesforce objects with associated status
mySourceId
},
5); // Maximum number of records to return

Step 8. 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.

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

Apex
RecipientTabsExample.sendEnvelopeMethod();

Next steps