Sending a DocuSign Template with Apex Toolkit

“So what is the DocuSign Apex Toolkit?” is a question I get a lot! The Apex Toolkit is a toolkit for Salesforce developers to integrate DocuSign into Salesforce apps and processes. Similar to an SDK, our Apex Toolkit exposes a full set of programmatic objects and methods that you call from your Apex code. The Apex Toolkit is installed with DocuSign eSignature for Salesforce Essentials, available as a managed package on the Salesforce AppExchange.

Clear as mud? Great! Now let me show you how to use it!

To use the Apex Toolkit, there are a few prerequisites:

  1. You will need a Salesforce org; a Salesforce developer account will work.
  2. Don’t forget to enable My Domain in your developer org!
  3. A free DocuSign developer sandbox account. You can sign up for a sandbox account on the DocuSign Developer Center.
  4. Install DocuSign eSignature for Salesforce Essentials from the Salesforce AppExchange.
  5. Complete the integration of DocuSign into Salesforce.

Once all the above is complete, you can get started sending an envelope with the Apex Toolkit. Log into Salesforce, head over to Set up home page, and in the quick find box, type Apex Classes, select the result to open Apex Classes in Salesforce. Once you are in Apex Classes, select “New” to create a new public class. Public classes are created so that the logic encapsulated in that class can be publically used by other Apex classes in your Salesforce organization.

Creating a new public class

 

Create your public class and name it however you choose. Then create your public static method. Creating a static method for this demo encapsulates the logic of envelope sending so that you can directly access the method, like this:

ApexToolkitDemo.sendEnvelopeMethod(), without the need to create an instance explicitly.

Now you can use the DocuSign EnvelopeService.sendEnvelope method to create an empty envelope template definition that will be subsequently completed, created, and sent. Here, you specify the source ID (Salesforce ID) of the record in the entity parameter which is to be used as the source object for the envelope.

This code snippet shows how to create an empty envelope:



public class ApexToolkitDemo {

public static void sendEnvelopeMethod() {

Id mySourceId; // 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.

// Use myEnvelope for later


Next, specify from where to pull the recipient data for the envelope. This will contain the full set of parameters for the recipient's role in the signing process, including their role, authentication data, status in the workflow, and contact settings.

In this example, the envelope will have one recipient: a document signer.

This code snippet shows how to add a contact as a recipient:



// Use a Salesforce contact record as a Recipient here

Contact myContact = [SELECT Id, Name, Email FROM Contact LIMIT 1];

// 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 });


You need to tell the code which template to use. To find a template ID in a DocuSign account, go to the top nav and find Templates, select the template you wish to use, and click the “I” icon to see the ID. To create a new document, add the template ID to the envelope using the Document.fromTemplate method and pass in the ID of a valid DocuSign template. Then, use the Envelope's withDocuments method to add the document to the envelope.

Finding the template ID

 

This code snippet shows how to add a document to an envelope from a template:



// myTemplateId contains the DocuSign ID of the DocuSign Template

dfsle.UUID myTemplateId = dfsle.UUID.parse('01234567-xxxx-xxxx-xxxx-456789abcdef');

// Create a new document for the Envelope

dfsle.Document myDocument = dfsle.Document.fromTemplate(

myTemplateId, // The templateId in dfsle.UUID format

'myTemplate'); // Name of the template

// Add document to the Envelope

myEnvelope = myEnvelope.withDocuments(new List<dfsle.Document> { myDocument });


Once that is all completed, add the EnvelopeService.sendEnvelope method. This passes the set of envelope data that was completed in the previous three steps with a sendNow parameter value of “true”, indicating that the envelope should be sent immediately rather than be saved as a draft. If you want to save as a draft add the value “false”.

This code snippet shows how to send the envelope:



// Send the envelope

myEnvelope = dfsle.EnvelopeService.sendEnvelope(

myEnvelope, // The envelope to send

true); // Send now


Now test that it's all working! Head over to developer console in Salesforce by clicking Developer Console under the quick access menu (

Quick access menu

) or your name. When it opens, select Debug > “open execute anonymous window” to run your code.

Type in your class name and execute the method:

Typing in your class name

 

TA-DA! Your envelope is now in the mailbox of the recipient!

Your envelope in the recipient's inbox

 

Want to learn more about the Apex Toolkit? Check out our free course, DocuSign eSignature for Salesforce Essentials: Apex Toolkit.

Don’t want to read? Here is a video where I walk you through these steps:

 

 

 

Lauren Dunne

Lauren Dunne is our Senior Salesforce Evangelist and self-confessed Salesforce geek! She has ten years of Salesforce expertise, and her past roles range from Solution Architect to “Admineloper.” She is a 2x Salesforce MVP and founder and host of Ohana Coffee, a global virtual meet-up. She even has a Salesforce tattoo!

 

 

 

Additional resources

LaurenBolopue
Author
Lauren Bolopue
Lead Product Evangelist
Published