Hello world with the Apex Toolkit

The Apex Toolkit makes it easy to build integrations with DocuSign and Salesforce using Apex code. To get you started, I’ll walk you through installing the Apex Toolkit and writing your first code to send an envelope.

Installing the Toolkit 

  1. Log in to your custom Salesforce domain to configure the Apex Toolkit.
  2. Navigate to the Salesforce App Exchange and enter “Docusign eSignature for Salesforce” in the search box. 
  3. Select and install DocuSign eSignature for Salesforce Essentials: Finding DocuSign eSignature for Salesforce Essentials on the Salesforce AppExchange

    While going through the installation steps, you will need to approve the requested third-party access: 

    Approving third-party access
  4. Check that the DocuSign Apps Launcher package is installed. To do so, navigate to Setup in Salesforce. In the search box, type Installed Packages, then select Installed Packages. After the installation is complete, the DocuSign Apps Launcher app will be available in your Salesforce org’s list of installed packages. Salesforce Setup: Installed packages

Connect your Salesforce and Docusign accounts

Next, connect your DocuSign account to your Salesforce instance. If you do not have a DocuSign Developer account, you can create one for free.

  1. Navigate to Setup in Salesforce, click the App Launcher button, type DocuSign, then click DocuSign Apps Launcher from the list. 
  2. When you open the DocuSign Apps Launcher app for the first time, you need to log in to your DocuSign account. Click Advanced Options, then click Log in to Demo AccountSalesforce: Log in to your DocuSign account
  3. Log in with your DocuSign developer account credentials, then click Accept to grant the requested permissions. 
  4. Under Select Account, be sure your account is selected; then click Next. Connect DocuSign to Salesforce

Hello World! Sending an envelope with the Apex Toolkit

The DocuSign Apps Launcher package includes the Apex Toolkit. Now that your Salesforce and DocuSign accounts are linked, you’re ready to send a Hello World envelope. For your first envelope, you will use a Salesforce Account as the source object.

You will use a file located in a Salesforce Library. Once you have added the file to a library, please take note of the DocumentId, which you can see in the URL of the selected document. 

Finding the DocumentId

You will also need to have an Account object set up for this demonstration. Please view this tutorial on how to set up an Account object in Sales Cloud. 

For Apex programming, I recommend using Visual Studio Code with the SFDX plugin. If you do not have this plugin, simply open the Developer Console from within Salesforce, copy in the following code, and run it:

final String SIGNER_NAME = 'Signer name';
final String SIGNER_EMAIL = 'signer_email@example.com';
final string DOCUMENT_ID = '0693V000006vf4q';
        
final Account myAccount = [SELECT Id FROM Account WHERE Name = 'Edge Communications'
    LIMIT 1
    ];
 
//The ID of a document stored in a Salesforce library
final Id myFileId = [SELECT id from ContentVersion where ContentDocumentId = '0693V000006vf4q'
    LIMIT 1
    ].id;
       
// Create an empty envelope It needs to be associated with a
// Salesforce object. We’re using an account object.    
dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
  new dfsle.Entity(myAccount.Id));
 
//Returns a list of Documents with the given fileID(s)
final List<dfsle.Document> doc = dfsle.DocumentService.getDocuments
   (ContentVersion.getSObjectType(), new Set <Id> {
   myFileId
});
 
//adds document to the Envelope
myEnvelope = myEnvelope.withDocuments(doc);
 
// Create an Signature tab
dfsle.Tab mySignatureTab = new dfsle.SignHereTab()
    .withRequired(true) // Signing mandatory
    .withPosition(new dfsle.Tab.Position(
     1, // The document to use
     1, // Page number on the document
     193, // X position
     168, // Y position
     null, // Default width
    null)); // Default height
 
dfsle.Recipient myRecipient = dfsle.Recipient.fromSource(
    SIGNER_NAME, // Signer name
    SIGNER_EMAIL, // Signer email
    null, // Signer phone number
    'null', // Signer role parameter is required
    null) // No Salesforce association
    .withTabs(new List < dfsle.Tab > { // Associate the tabs with this recipient
    mySignatureTab
    });
 
// Add recipient to the envelope
myEnvelope = myEnvelope.withRecipients(new List < dfsle.Recipient > {
    myRecipient
    });
 
myEnvelope = dfsle.EnvelopeService.sendEnvelope(
    myEnvelope, // The envelope to send
    true);

Sending the envelope

Once you have the code entered in the Developer Console, select Execute at the bottom right of the console.

Executing your code in the Salesforce Developer console

You should see a “Success” status in the console window once your code has run.

Success status message

The document will be delivered to the intended signer’s email address. 

Resulting document: 

Resulting document

Summary 

Now you know how to send a simple envelope by leveraging the Apex Toolkit. If you have any issues, please do not hesitate to post on our Stack Overflow community with the tag docusignapi or contact us via Docusign Support. 

Additional resources

Ben Dowling
Author
Ben Dowling
Developer Support Engineer
Published