> You are reading the Docusign Developer Documentation. For clean Markdown of any page, append .md to the page URL.
>
> Documentation indexes by section:
> - APIs and SDKs: https://docusign.docs.buildwithfern.com/docs/llms.txt
> - Platform 101: https://docusign.docs.buildwithfern.com/platform/llms.txt
> - Extension Apps: https://docusign.docs.buildwithfern.com/extension-apps/llms.txt
> - Docusign CLI: https://docusign.docs.buildwithfern.com/docusign-cli/llms.txt
>
> Authentication (OAuth, JWT Grant, M2M): https://docusign.docs.buildwithfern.com/platform/auth
> Webhooks and Connect: https://docusign.docs.buildwithfern.com/platform/webhooks
>
> OpenAPI specifications: https://docusign.docs.buildwithfern.com/openapi.json
> MCP server: https://docusign.docs.buildwithfern.com/_mcp/server

# How to bulk send envelopes

> Demonstrates how to send envelopes in bulk to multiple recipients using the Apex Toolkit.

This topic demonstrates how to send envelopes in bulk to multiple recipients using the [Apex Toolkit](/docs/salesforce/salesforce101/apex-toolkit). Using the [dfsle.BulkSendService](/docs/salesforce/apex-toolkit-reference/bulksendservice.html) class, this code demonstrates how to create and bulk send envelopes to each member of a [Salesforce Chatter](https://www.salesforce.com/products/chatter/overview/) group.

**Note:** To learn about bulk sending with the eSignature REST API, see our [guide](/docs/esign-rest-api/esign101/concepts/envelopes/bulk-send) and [how-to](/docs/esign-rest-api/how-to/bulk-send-envelopes).

The underlying Apex code shown in this example can be used in Lightning applications or components, Visualforce pages, Communities, and any other component that invokes Apex code in Salesforce.

## Prerequisites

1. Install the [Docusign eSignature for Salesforce](/docs/salesforce/how-to/apex-toolkit-install)  managed package.
2. Create a subdomain for your Salesforce organization by enabling "My Domain."
3. Select the **Docusign Setup** tab and complete the login step.
4. [Create a Chatter group](https://help.salesforce.com/s/articleView?id=sf.collab_group_creating.htm\&type=5) containing the contacts that you want to send your documents to.
5. Create a template in your Salesforce org with a role of '`SignerOne`':

   1. Open the Docusign Apps Launcher and go to the **Docusign Envelope Templates** tab.
   2. Choose **New** to create a new template.
   3. Give your template a name and choose a data source.
   4. Add a recipient to the template. Choose **By Role** under **Other Sources** and give the recipient a role of "`SignerOne`."
   5. Follow the steps to set the template options and add tabs or merge fields. Then **Save and Close** the template.

## Required data

Running the code in this how-to requires this data:

| Data element     | Description                                          |
| ---------------- | ---------------------------------------------------- |
| Chatter group ID | The ID of a Salesforce Chatter group.                |
| Template ID      | The ID of a template created in your Salesforce org. |

## Step 1. Define your class and methods

Create the class and methods that will contain the code in this example. You can skip this step if you are adding this code to an existing class.

**Note**: The steps outlined in this example need to be defined in separate methods because Apex does not allow you to update a record with a Data Manipulation Language statement within the same transaction as a Web Service Callout.

Step 2 of this example includes a DML insert that requires the code in that step to be executed separately from the callout in Step 3.

**`Apex`**

```java title="Apex"
public with sharing class BulkSendingClass{
}
```

## Step 2. Create contact list for bulk sending

Create the bulk send list with the contact information for each recipient. This list may be based on any group construct and contains copies of the envelope to be sent. A recipient note, authentication options, and email subject and message may be set for each copy, but each copy must have exactly one recipient.

The accompanying code demonstrates how to create a new envelope definition for each member of the specified Chatter group and how to create a contact list which will be used for bulk sending those envelopes. This example uses a Chatter group to generate the bulk copies, but you can take contact information from any data source to create the recipients with the [dfsle.Recipient.newBulkRecipient](/docs/salesforce/apex-toolkit-reference/recipient.html) method. Be sure to add the `'SignerOne'` role to the recipients using the `withRole` method to match the role in your template.

Be sure to replace the value for `myGroupId` with your own Chatter group ID. To find your Chatter group ID, go to the Docusign Apps Launcher, search for Groups, and select your Chatter group. Copy the ID from the URL of your Chatter group page.

**Note:** Each created bulk list persists both in Salesforce and Docusign and may be reused to send multiple envelopes. The list definition can be updated when the underlying group membership changes with  [dfsle.BulkSendService.updateLists](/docs/salesforce/apex-toolkit-reference/bulksendservice.html), or it may be removed with [dfsle.BulkSendService.deleteLists](/docs/salesforce/apex-toolkit-reference/bulksendservice.html) when no longer relevant.

**`Apex`**

```java title="Apex"
@AuraEnabled
public static Id buildList(){

    Id myGroupId = '0FXXXXXXXXXXXXXXAC'; // Substitute this value with your Chatter group ID
    // Build a list membership from a Chatter group
    // Recipient authentication, email settings, or private notes may be overridden using the Recipient.with* methods
    // Envelope email settings and notifications may also be overridden per copy using the Envelope.with* methods

    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;

}
```

## Step 3. Create and send the bulk envelope

Once a list has been created, create an envelope with the document(s) you wish to send. You can use a Docusign [template](/docs/esign-rest-api/esign101/concepts/templates) as the document or use a document that leverages [Auto-place (anchor tagging)](/docs/esign-rest-api/esign101/concepts/tabs/auto-place) to add a recipient’s information automatically to generic tags within a document.

Be sure to replace the value for `myTemplateId` with the ID of a template in your Salesforce org. To find the template ID, go to the Docusign Apps Launcher and choose the **Docusign Envelope Templates** tab. Select the template you want to use and copy the **Docusign template ID** GUID under the **Details** tab.

Next, use the [dfsle.BulkSendService.sendEnvelope](/docs/salesforce/apex-toolkit-reference/bulksendservice.html) method to bulk send the envelope to each member in the list you created previously, as demonstrated in the accompanying code. These steps need to be done within an  [@future](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm) method to prevent the code from failing due to Apex's DML and callout limitations.

**`Apex`**

```java title="Apex"
@future(callout=true)
/**
*  bulkSendEnvelopes method
*    Use myListId from buildList method
*    Assign myStatusId to the result of the BulkSendService call
*/
public static void bulkSendEnvelopes(Id myListId){

 dfsle.UUID myTemplateId = dfsle.UUID.parse('3d9fac4b-xxxx-xxxx-xxxx-6cd70d70f8ed'); // Substitute with your template ID
 dfsle.Document myDocument = dfsle.Document.fromTemplate(
	    myTemplateId, // templateId in dfsle.UUID format
	    'myTemplate'); // name of the template

 // Create the draft bulk envelope. This will have exactly one placeholder bulk recipient
 dfsle.Envelope myEnvelope = dfsle.BulkSendService.getDraftEnvelope(
 new List<dfsle.Document> { myDocument },	null); // Optional Salesforce source entity

 // Send envelope with a placeholder bulk recipient
 dfsle.BulkList.Result myResult = dfsle.BulkSendService.sendEnvelope(myListId, myEnvelope);

}
```

## Step 4. Request bulk status

Once the bulk send job has been queued successfully, you can periodically check the status of the job using [dfsle.BulkSendService.getStatus](/docs/salesforce/apex-toolkit-reference/bulksendservice.html) method.

**`Apex`**

```java title="Apex"
// Retrieve status from the API
dfsle.BulkList.Status myStatus = dfsle.BulkSendService.getStatus(new Set<Id> { myStatusId })[0];
// Do something with the returned status
if (myStatus.queuedEnvelopes > 0) {
    // Still processing...
} else if (myStatus.failedEnvelopes > 0) {
    // One or more envelope copies failed to send
    for (dfsle.BulkList.StatusDetail sd : myStatus.details) {
        // Handle send failures
    }
}
```

## Step 5. Execute your methods

To execute your code, go to the [Developer Console](https://help.salesforce.com/s/articleView?language=en_US\&id=sf.code_dev_console.htm\&type=5) in Salesforce, open the **Debug** menu, then select **Open Execute Anonymous Window**. Type the accompanying code in the window and select **Execute**.

> **Note**
>
> View the source code for this how-to on GitHub: [Apex](https://github.com/docusign/code-examples-apex/blob/master/force-app/main/default/classes/BulkSendingController.cls)

**`Apex`**

```java title="Apex"
Id myListId = BulkSendingClass.buildList();
BulkSendingClass.bulkSendEnvelopes(myListId);
```

## Expected response

Each member of the Chatter group will receive an email asking them to sign the document in your template.

## Next steps

* See all available Apex Toolkit [How-to guides](/docs/salesforce/how-to).
* See [How to perform unit testing](/docs/salesforce/how-to/unit-testing) to ensure that you have adequate code coverage.