Sending a Docusign Template with Apex Toolkit
Learn more about how to take advantage of Docusign eSignature for Salesforce Essentials with Apex Toolkit
Table of contents
“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:
You will need a Salesforce org; a Salesforce developer account will work.
Don’t forget to enable My Domain in your developer org!
A free Docusign developer sandbox account. You can sign up for a sandbox account on the Docusign Developer Center.
Install Docusign eSignature for Salesforce Essentials from the Salesforce AppExchange.
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.
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 { 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](https://developers.docusign.com/salesforce/apex-toolkit-reference/document.html)
method and pass in the ID of a valid Docusign template. Then, use the Envelope's [withDocuments](https://developers.docusign.com/salesforce/apex-toolkit-reference/envelopeservice.html)
method to add the document to the envelope.
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 { myDocument });
Once that is all completed, add the [EnvelopeService.sendEnvelope](https://developers.docusign.com/salesforce/apex-toolkit-reference/envelopeservice.html)
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 (
) 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:
TA-DA! Your envelope is now in the mailbox of the recipient!
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 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
Lauren Bolopue is our Lead Product Evangelist and self-confessed Salesforce geek! She has eleven years of Salesforce expertise, and five years of Docusign expertise, and her past roles range from Solution Architect to “Admineloper.” She is a Salesforce MVP alum and founder and host of Ohana Coffee, and Docusign Meetups global virtual meet-ups. She even has a Salesforce tattoo!
Related posts