By Baxter Finch, Developer Support Engineer

The Apex Toolkit was built to provide Salesforce developers a way to interact with the DocuSign eSignature REST API using the Salesforce Apex programming language. This wrapper allows developers to gather information within Salesforce, create, and send out envelopes based on that information. For example, a customer might want to access a specific Opportunity within Salesforce and send a certain document to each Opportunity contact for a signature. In this demonstration, I will be showing you how to gather Contacts from a specific Opportunity and, utilizing the BulkSendService, generate a list of recipients and send out envelopes to each of them.

In this first chunk of code, I am creating an empty list of Envelopes using the dfsle object and storing this in the myBulkCopies variable. I then gather all the Contacts from an Opportunity, storing them in oppContactRoles, and finally, I start looping through the list of contacts to build each envelope:

   List myBulkCopies = new List();   
   List oppContactRoles = [
      SELECT ContactId FROM OpportunityContactRole 
      WHERE OpportunityId = ''
   ];
   for (OpportunityContactRole ocrs : [
      SELECT ContactId FROM OpportunityContactRole
      WHERE OpportunityId = ''
   ]) {
      Contact contact = [SELECT Name, Email, Id FROM Contact WHERE Id = : ocrs.ContactId];
      myBulkCopies.add(dfsle.Envelope.newBulkCopy(
         dfsle.Recipient.newBulkRecipient(
            contact.Name, // Name
            contact.Email, // Email
            new dfsle.Entity(contact.Id))));
   }
   dfsle.BulkList myList = dfsle.BulkSendService.createLists(new List {
      dfsle.BulkList.newList(
         'My bulk list', // List name
         myBulkCopies, // Envelope copies
         new dfsle.Entity('')) // The Salesforce source object
    })[0];
    Id myListId = myList.id;

Inside the loop, I initiate another OSQL query, retrieving the Contact’s full Salesforce Object information. Then, I create a newBulkRecipient which is a method attached to the dfsle.Recipient object, and pass in the Contact’s Name, Email, and Salesforce Entity Object reference ID. The newBulkRecipient is passed into the Envelope.newBulkCopy method, which creates the envelope’s shell and is then added to themyBulkCopies list of envelopes.

Once this process is completed for every Contact in the opportunity, I pass the myBulkCopies list into the dfsle.BulkSendService.createLists method. The list of envelopes is now created and a reference to the list’s ID is stored in the myListId variable. The next chunk of code takes this list ID and sends the envelopes using the BulkSendService class:

   // Created by first chunk of code
   Id myListId =’’; 
   public class ProcessBulkEnvelopes {
      public Id myListId;
      public dfsle.Envelope draftEnvelope;
      public dfsle.BulkList.Result finishedEnvelope;
      public ProcessBulkEnvelopes(Id myFileId, Id myListId) {
         this.myListId = myListId;
         try {
            List draftDocs = dfsle.DocumentService.getDocuments(
               ContentVersion.getSObjectType(), new Set { myFileId });
            this.draftEnvelope = dfsle.BulkSendService.getDraftEnvelope(draftDocs, null);
         } catch(Exception ex){
            System.debug('BulkSendService.getDraftEnvelope Failure: ' +
              ex.getMessage());
         }
      }
      private void SendEnvelope() {
         try {
            this.finishedEnvelope = dfsle.BulkSendService.sendEnvelope(this.myListId, 
               this.draftEnvelope);
         } catch(Exception ex){
            System.debug('dfsle.BulkSendService.sendEnvelope Failure: ' +
               ex.getMessage());
         }
      }
   }
   try {
      // Send the Document Version ID and the myListId into GetDraftClass to build out the draft envelope
      ProcessBulkEnvelopes draftEnvelope = new 
         ProcessBulkEnvelopes('', myListId);
      draftEnvelope.SendEnvelope();
   }
   catch (Exception ex) {
      system.debug(ex.getMessage());
   }

This code creates a ProcessBulkEnvelopes class with a constructor that takes in myFileId (a Salesforce versioned Document) and myListId (the ID of the list created by the first chunk of code). It then creates a draftDocs variable using myFieldId and passes this into the dfsle.BulkSendService.getDraftEnvelope method. Once the draftEnvelope is finished, I then call SendEnvelope(), which references the myListId variable and the draftEnvelope I just created, sending the envelopes to each of the recipients stored in the saved list.

This demonstration explores methods from several available services built into the Apex Toolkit, including BulkSendService, DocumentService, and EnvelopeService. Here are some common services available in the Apex Toolkit:

I also covered some simple OSQL queries to demonstrate how you can interact with Salesforce data. If you have any questions on troubleshooting the Apex Toolkit, reach out to DocuSign Developer Support at [email protected].

Additional Resources

Tags