How to use embedded signing
This topic demonstrates how to write Apex code that will use Embedded signing to create and send a Docusign envelope to the current Salesforce user for immediate signing within Salesforce.
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. If you are using this code in a Salesforce Community, make sure that you have satisfied the permission prerequisites described below.
Video walkthrough
Prerequisites
- Install the Docusign eSignature for Salesforce managed package.
- Create a subdomain for your Salesforce organization by enabling “My Domain.”
- Select the Docusign Setup tab and complete the login step.
- In the Docusign Setup tab, choose Configuration in the left menu, then go to the Settings tab and choose a user next to Enable System sender. This allows Community users to send envelopes even if they are not a member of the Docusign account. In that case, envelopes will be sent from the admin user that you selected.
- Make sure that the sender’s account is assigned the Docusign Sender permission set. This gives them permissions to perform basic Docusign sending and signing permissions in Salesforce. This permission can only be granted by a Salesforce administrator, which is typically assigned when provisioning the user account. Docusign senders differ from users in that they do not have permission to access methods for retrieving envelope status.
- Create a Salesforce DX project. This requires that you have already set up Visual Studio Code with the necessary Salesforce development tools. You will be using Visual Studio Code to create a Lightning Web Component that will initiate your embedded signing session because Salesforce does not support development of Lightning Components in the Developer Console.
Required data
Running the code in this how-to requires this data:
Step 1. Define your class
Create the class that will contain most of the code in this how-to. To create a class in Visual Studio Code, navigate to the force-app/main/default/classes folder in your SFDX project structure, then right-click classes and select SFDX: Create Apex Class.
Step 2. Create and send an envelope
Before you can start the embedded signing process, you must first create a Docusign envelope and populate it with at least one document and recipient.
-
Use the dfsle.EnvelopeService.getEmptyEnvelope method to create an empty envelope template definition that can be subsequently completed, created, and sent.
-
Use the dfsle.Document.fromTemplate method to create a new document using a template in your Docusign account.
-
Create a dfsle.Recipient object for each recipient of the envelope using the dfsle.Recipient.newEmbeddedSigner method, which indicates that the process will use embedded signing (as opposed to requesting a signature via email).
In this example, the envelope will have one recipient, a document signer. Once a recipient is created, use the
withRecipientsmethod to add it to the envelope. -
Add the documents and recipients to the envelope and use the dfsle.EnvelopeService.sendEnvelope method to send it.
Step 3. Host an embedded signing session
After the envelope is sent, initiate an embedded signing session. The Apex Toolkit will verify that the current recipient matches the current Salesforce user and request a link to a one-time signing session.
The user will be redirected to this link to view or sign the envelope document(s). After signing is complete, the user is redirected back to the Salesforce application automatically.
Step 4. Create a Lightning Web Component
To create a new Lightning Web Component:
- Navigate to the force-app/main/default/lwc folder in the menu on the left.
- Right-click lwc and select SFDX: Create Lightning Web Component. This will generate the files that you need to create your component.
- Open the .js-meta.xml file to see metadata about your new component.
- Set the
isExposedfield totrueand add targets forlightning__AppPage,lightning__RecordPage, andlightning__HomePageto ensure that your component can be added to pages in your org.
Step 5. Edit your component’s HTML
Open the HTML file under your component. You can use this file to style your Lightning Web Component however you like.
Step 6. Use your Lightning Web Component to initiate embedded signing
Finally, edit your Lightning Web Component’s JavaScript file so that the user’s interaction with the component executes the methods you defined in the previous steps.
- Open the JS file under your component.
- Import the
sendEnvelopeandgetEmbeddedSigningUrlmethods from yourEmbeddedSigningControllerclass. - Save the ID of the Docusign template that you want to use to the
templatevariable. - Use JavaScript’s
handleClickmethod to initiate embedded signing by calling yoursendEnvelopemethod, getting an embedded signing URL, and redirecting the browser to the signing URL.
When you’re finished editing your component, you will deploy your source code to your Salesforce org. To do this, navigate to the force-app/main/default folder in the menu on the left and right-click default, then select SFDX: Deploy Source to Org.
Now, when you edit any page in your Salesforce org, you will see your new Lightning Web Component in the Lightning App Builder. You can add your component to a page and use it to initiate an embedded signing session.
View the source code for this how-to on GitHub: Apex
Next steps
- See all available Apex Toolkit How-to guides.
- Learn more about Embedded signing and sending.
- See How to perform unit testing to ensure that you have adequate code coverage.