> 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 send an envelope from a Salesforce flow

> Learn how to send an envelope from a flow created with the Salesforce Flow Builder.

This topic demonstrates how to send an envelope from a flow created with the Salesforce Flow Builder. The flow created in this example uses the Apex class defined below to send an envelope every time a new Contact object is created 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. In your Docusign account, [define the template](/docs/esign-rest-api/how-to/create-template) for the envelope that you want to send. You can create this template using the Docusign REST API or through the [Docusign Apps Launcher](https://support.docusign.com/s/document-item?language=en_US\&bundleId=srl1586134368658\&topicId=gou1591745811909.html&_LANG=enus).

## Required data

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

| Data element   | Description                                                                                                                                                                                                                                                                                              |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| \{TEMPLATE_ID} | A [GUID](http://guid.one/) value that identifies a Docusign [template](/docs/esign-rest-api/esign101/concepts/templates) that is used to create a Docusign [envelope](/docs/esign-rest-api/esign101/concepts/envelopes). This value is automatically generated by Docusign when the template is created. |

## Step 1. Define your class

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

**`Apex`**

```java title="Apex"
public class SendToNewContact {
}
```

## Step 2. Create a method that sends an envelope

Create a method that will send an envelope using [dfsle.EnvelopeService.sendEnvelope](/docs/salesforce/apex-toolkit-reference/envelopeservice.html). The method takes the source ID of the new contact as a parameter and uses that to get the recipient’s name and email.

The accompanying code demonstrates how to create a new envelope, add a recipient to the envelope, and create a document from a Docusign template. Make sure to replace `'8dafd5dd-xxxx-xxxx-xxxx-542f35b227f7'` with [your template ID](https://stackoverflow.com/questions/32106910/where-is-docusign-templateid).

**`Apex`**

```java title="Apex"
public static Id createAndSendEnvelope(Id mySourceId) {
    dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
      new dfsle.Entity(mySourceId)
    );
    Contact myContact = [SELECT Name, Email FROM Contact WHERE Id = :mySourceId LIMIT 1];
    dfsle.Recipient myRecipient = dfsle.Recipient.fromSource(
      myContact.Name,
      myContact.Email,
      null,
      'Signer1',
      new dfsle.Entity(myContact.Id)
    );
    myEnvelope = myEnvelope.withRecipients(new List<dfsle.Recipient> { myRecipient });
    dfsle.UUID myTemplateId = dfsle.UUID.parse('8dafd5dd-xxxx-xxxx-xxxx-542f35b227f7');
    dfsle.Document myDocument = dfsle.Document.fromTemplate(
      myTemplateId,
      'Services Framework Agreement'
    );
    myEnvelope = myEnvelope.withDocuments(
      new List<dfsle.Document> { myDocument }
    );
    myEnvelope = dfsle.EnvelopeService.sendEnvelope(
      myEnvelope,
      true
    );
    return myEnvelope.Id;
  }
```

## Step 3. Create an @future method

Create a method with the [@future](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm) annotation that calls your `createAndSendEnvelope` method. The `@future` annotation prevents the code from failing due to Apex’s DML and callout limitations.

**`Apex`**

```java title="Apex"
@future(callout = true)
public static void sendEnvelopeInFuture(Id mySourceId) {
  createAndSendEnvelope(mySourceId);
}
```

## Step 4. Create an invocable method that sends the envelope

Create a method with the [@InvocableMethod](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm) annotation that calls your `@future` method. The `@InvocableMethod` annotation allows you to call your method from a flow using dynamic inputs specified by that flow.

**`Apex`**

```java title="Apex"
@InvocableMethod
public static List<String> sendEnvelope(List<Contact> record) {
  Contact mySource = record.get(0);
  sendEnvelopeInFuture(mySource.Id);
  return Null;
}
```

## Step 5. Create a record-triggered flow

Finally, create a record-triggered flow using Salesforce’s [Flow Builder](https://trailhead.salesforce.com/content/learn/trails/build-flows-with-flow-builder). Select **+ Choose Object**. In the **Choose Object** dialog, select **Contact** and **None** under **Condition Requirements**.

Add an Apex Action to your flow by dragging the **Action** element into the Flow Builder. In the **Action** search box, find your new Apex class and select it as your desired action. In the **Label** field, enter Send Envelope From Flow.

To pass the new contact’s source ID to your Apex method, drag the **Get Records** element from the Toolbox into the Flow Builder just above your **Action** element. In the **Label** field, enter Get Source ID. In the Object field, select `Contact`. Set the **Condition Requirements** so that the Id equals `{!$Record.Id}` which becomes **\$Record > Contact ID**.

Double-click on your **Apex Action** element. Under **Set Input Values**, move the slider to the right. In the record field, select **Contact from Get_Source_ID**.

The accompanying image demonstrates what your flow should look like. Be sure to drag arrows between the elements as shown in the image, **Save** as Send Envelope to New Contact, and select the **Activate** button.

![An example of the record-triggered flow in the Salesforce flow builder](/_fern-files/_img/dfbddee184a0cdaced7534b92db26f06ca2bc67d5ed4c8e6d4fad7b375f16ade.webp)

## Step 6. Activate the flow

Activate your flow by creating a new contact in your org. To create a new contact, from My Domain, select the App Launcher icon in the upper left corner > **Service** > **Contacts** > **New**. The envelope will be sent to the email address associated with the new contact.

> **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/SendToNewContact.cls)

![The Contact object that will activate the record-triggered flow](/_fern-files/_img/ff1ef1ce925ad95e7392d10dfdf927be557c92b730e45e742067d40a459d8490.webp)

## Next steps

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