Blog
Home/

Customize bulk sending for your Salesforce contacts

Author Paige Rossi
Paige RossiSr. Programmer Writer
Summary3 min read

Learn how to use the Apex Toolkit to bulk send envelopes to your Salesforce contacts in a way that works for your use case.

    • Build the recipient list
    • Send the bulk envelopes
    • Additional resources

    Table of contents

    The Apex Toolkit lets you integrate Docusign eSignature REST API functionality into Salesforce workflows and processes by exposing a full set of programmatic objects and methods that you can call from your Apex code. One of the most popular Apex Toolkit use cases is bulk sending, which lets you send an envelope to a list of recipients, each of whom will receive a copy to read or sign. If you’ve tried bulk sending with the Apex Toolkit, you may have seen our how-to guide on the subject. It demonstrates how to create and bulk send envelopes to the members of a Salesforce Chatter group. We often get questions about whether you can bulk send envelopes to recipients who are not all members of a Chatter group, and the answer is yes. In this blog post, I’ll go over our code example for bulk sending with the Apex Toolkit and explain how you can modify it to fit your use case.

    As shown in the how-to, there are two main steps for bulk sending. The first is to build your list of recipients and the second is to bulk send the envelopes to the recipients in that list.

    Build the recipient list

    In the how-to, we build the contact list by looking at each member of a Chatter group in a for loop and using their information to create a new recipient with the dfsle.Recipient.newBulkRecipient method. The code snippet below from the how-to demonstrates this.

    @AuraEnabled
    public static Id buildList(){
    
        Id myGroupId = '0FXXXXXXXXXXXXXXAC'; // Substitute this value with your Chatter group ID
        // Build a list membership from a Chatter group
                
        List<dfsle.Envelope> myBulkCopies = new List<dfsle.Envelope>();
        for (CollaborationGroupMember m : [
            SELECT Member.Id, Member.Name, Member.Email
            FROM CollaborationGroupMember
            WHERE CollaborationGroupId = :myGroupId
        ]) {
            myBulkCopies.add(dfsle.Envelope.newBulkCopy(
                dfsle.Recipient.newBulkRecipient(
                    m.Member.Name,
                    m.Member.Email,
                    new dfsle.Entity(m.Member.Id) // Source Salesforce object
                )
            .withRole('SignerOne')
            ));
            }
                
        // Create the bulk list. This list persists after sending and may be reused for multiple batches of envelopes
        dfsle.BulkList myList = dfsle.BulkSendService.createLists(new List<dfsle.BulkList> {
            dfsle.BulkList.newList(
                'My bulk list', // List name
                myBulkCopies, // Envelope copies
                new dfsle.Entity(myGroupId)) // The Salesforce source object
            })[0];
                
        // Save the ID for later operations
        Id myListId = myList.id;
        System.debug(LoggingLevel.INFO, myListId);
        return myListId;
    
    }
    

    In the snippet above, the for loop includes an SOQL (Salesforce Object Query Language) query that returns CollaborationGroupMember objects. It then creates a dfsle.Envelope.newBulkCopy object for each recipient returned from the SOQL query.

    When you create a newBulkRecipient object, you can get the data to create that recipient from any source; it doesn’t have to be related to a CollaborationGroupMember. For example, to include recipients in the bulk send who are not Chatter group members, you can modify the for loop in the code above to instead choose Contact objects from Salesforce. The code snippet below demonstrates how to build a bulk list using contacts that were created this month.

    for (Contact m : [
        SELECT Contact.Id, Contact.Name, Contact.Email
        FROM Contact
        WHERE CreatedDate > THIS_MONTH
    ]) {
      myBulkCopies.add(dfsle.Envelope.newBulkCopy(
        dfsle.Recipient.newBulkRecipient(
          m.Name, // Name
          m.Email, // Email
          new dfsle.Entity(m.Id) // Source Salesforce object
        )
      .withRole('SignerOne')
    ));
    

    You can edit the SOQL query above with whatever conditions you need to build the list of Salesforce objects that you will use to create your bulk recipients.

    Send the bulk envelopes

    After you’ve built your list of bulk recipients, you need to send your bulk envelopes to those recipients. The code snippet below from the how-to guide demonstrates this.

    @future(callout=true)    
    public static void bulkSendEnvelopes(Id myListId){
        
        dfsle.UUID myTemplateId = dfsle.UUID.parse('3d9fac4b-xxxx-xxxx-xxxx-6cd70d70f8ed'); // Substitute your template ID
        dfsle.Document myDocument = dfsle.Document.fromTemplate(
        myTemplateId, // templateId in dfsle.UUID format
        'myTemplate'); // name of the template
    
        dfsle.Envelope myEnvelope = dfsle.BulkSendService.getDraftEnvelope(
        new List<dfsle.Document> { myDocument },	null); // Optional Salesforce source entity
    
        dfsle.BulkList.Result myResult = dfsle.BulkSendService.sendEnvelope(myListId, myEnvelope);
    
    }
    

    Just as when building your list of bulk recipients, you have options when creating your bulk envelope. In the snippet above, the envelope is created using a document from a template. Templates are useful for bulk sending because they provide you with a flexible document that can be reused for many bulk envelopes. However, if you would prefer to create your document from a file stored in Salesforce, you can edit the code above and replace the dfsle.Document.fromTemplate method with the fromFile method. 

    Check out How to bulk send envelopes on the Docusign Developer Center for more details on bulk sending with the Apex Toolkit, and check out the Apex Toolkit reference to learn more about the different ways that you can tailor our code examples to your specific use cases.

    Additional resources

    Author Paige Rossi
    Paige RossiSr. Programmer Writer

    Paige has been working for Docusign since 2020. As a Sr. Programmer Writer on the Developer Content team, she writes content and code to help developers learn how to use Docusign APIs.

    More posts from this author

    Related posts

    • Leveraging Docusign AI and Salesforce for Improved Contract Management
      Developers

      Leveraging Docusign AI and Salesforce for Improved Contract Management

      Author Subbarao Pydikondala
      Subbarao Pydikondala
    • Event Notifications using JSON SIM and HMAC

      Event Notifications using JSON SIM and HMAC

      Author Jonathan Sammons
      Jonathan Sammons
    Leveraging Docusign AI and Salesforce for Improved Contract Management

    Leveraging Docusign AI and Salesforce for Improved Contract Management

    Author Subbarao Pydikondala
    Subbarao Pydikondala
    Event Notifications using JSON SIM and HMAC

    Event Notifications using JSON SIM and HMAC

    Author Jonathan Sammons
    Jonathan Sammons

    Discover what's new with Docusign IAM or start with eSignature for free

    Explore Docusign IAMTry eSignature for Free
    Person smiling while presenting