Skip to navigation

How to send an envelope with email reminders

View as Markdown

This topic demonstrates how to send an envelope with email reminders using the Notifications class in the Apex Toolkit.

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
Contact IDThe ID of the Salesforce Contact that will receive the envelope.
Salesforce source IDThe Salesforce ID of the object from which the Docusign envelopes will be sent.
{TEMPLATE_ID}A GUID value that identifies a Docusign template that is used to create a Docusign envelope. This value is automatically generated by Docusign when the template is created.

Step 1. Define a public class and method

Create the class and methods 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 class SendEnvelopeWithReminderController {
public static void sendEnvelopeMethod(){
}
}

Step 2. Create the notification definition

Create a Notifications object to define the notification settings for all of the envelope’s recipients. The first parameter of the Notifications object is set to true to indicate that reminders should be sent to the recipients. The second parameter indicates the number of days that will pass before the first reminder is sent, and the third parameter indicates the number of days that will pass between reminders. The Notifications object also allows you to set an expiration date for the envelope and send a reminder before that date.

Apex
dfsle.Notifications notifySigner = new dfsle.Notifications(
true, // Indicates that the recipient should be reminded to sign
2, // Number of days to wait before sending a reminder
1, // Number of days between reminders
true, // Whether or not the envelope expires and is voided
90, // How many days before the envelope expires
10, // Number of days before expiration to remind the recipient
false // Placeholder for deprecated field
);

Step 3. Create the envelope definition

Use the EnvelopeService.getEmptyEnvelope method to create an empty envelope template definition that will be subsequently completed, created, and sent. Specify the Salesforce ID of the record in the Entity parameter that is to be used as the source object for the envelope. Create a Recipient object for each recipient of the envelope. Once a recipient is created, use the withRecipients method to add it to the envelope. Create a new dfsle.Document using the dfsle.Document.fromTemplate method and add it to the envelope. Add your Notifications object from the previous step to the envelope with the withNotifications method.

Apex
// Create an empty envelope.
dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
new dfsle.Entity(mySourceId));
// The initiating Salesforce entity.
// We will use a Salesforce contact record as a Recipient here
// We will use a Salesforce contact record as a Recipient here
Contact myContact = [SELECT Id, Name, Email FROM Contact WHERE Id = '00xxxxxxxxxxxxxxAR'];
// 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)); // Source object for the Recipient
// Add Recipient to the Envelope
myEnvelope = myEnvelope.withRecipients(new List<dfsle.Recipient> { myRecipient });
// myTemplateId contains the DocuSign Id of the DocuSign Template
dfsle.UUID myTemplateId = dfsle.UUID.parse('8dafxxxx-xxxx-xxxx-xxxx-xxxxxxxx27f7');
// Create a new document for the Envelope
dfsle.Document myDocument = dfsle.Document.fromTemplate(
myTemplateId, // templateId in dfsle.UUID format
'myTemplate'); // name of the template
// Add document to the Envelope
myEnvelope = myEnvelope.withDocuments(new List<dfsle.Document> { myDocument });
// Add notification to the envelope
myEnvelope = myEnvelope.withNotifications(notifySigner);

Step 4. Send the envelope

Send the envelope using the EnvelopeService.sendEnvelope method. Set the sendNow parameter 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.

The accompanying code demonstrates how to send the envelope.

Apex
myEnvelope = dfsle.EnvelopeService.sendEnvelope(
myEnvelope, // The envelope to send
true); // Indicates that the envelope should be sent now

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.

View this code on GitHub: Apex.

Apex
SendEnvelopeWithReminderController.sendEnvelopeMethod();

Expected response

If your API call is successful, the recipient should receive an email notification asking them to sign the document in the envelope. After two days, they will begin to receive daily email reminders until the envelope expires in 90 days. You can view the envelope in the Docusign Apps Launcher under Docusign Envelopes.

ApexSendWithEmailReminder.jpg

Next steps