Skip to main content

Here is my scenario:

In Salesforce, I have an Opportunity that can be related to multiple Opportunity Documents (custom object). There are three Opportunity Documents: Non-Disclosure Agreement, Contract, and Onboarding Policy Agreement. We created Gen templates for each of these.

Each document may have a different person that needs to sign. For example, Bob may need to sign the Non-Disclosure Agreement and Contract while Alice needs to sign the Onboarding Policy Agreement. As such, we have a "Signer" field captured on Onboarding Document.

Lastly, there could be multiple of one document on an Opportunity that needs to be signed. For instance, we may have two copies of a Non-Disclosure Agreement that needs signature for one opportunity.

Our thinking is that we could create an LWC that leverages the apex toolkit to allow a user to, at any point, select one or more Opportunity Documents and then click a button called Generate Envelope. Using the Bob/Alice example above, the LWC (referencing the apex toolkit) would:

  1. Create an envelope
  2. See that the signers for all the documents are Bob and Alice, and therefore create a recipient for Bob and a recipient for Alice.
  3. Generate each document using the corresponding gen template 3a. For each document, grab the Signer and set its signature tag to the appropriate recipient. (ex: Bob is the Signer for Non-Disclosure Agreement, so Bob's recipientId would be set to the signature tag in the Non-Disclosure Agreement. Alice is the Signer for Onboarding Policy Agreement, so Alice's recipientId would be set to the signature tag in the Onboarding Policy Agreement. Bob is the Signer for Contract, so Bob's recipientId would be set to the signature tag in the Contract).
  4. Redirect user to sender view so they can review everything before actually sending.

Here is what I don't want: I don't want Bob to be two recipients in this scenario. I want Bob to be one recipient that gets tied to the signature tags for two documents so he only gets one email and can provide all his signatures in that one email.

Is this possible using the apex toolkit?

I know this is going to require the apex toolkit because this scenario is much more dynamic than what the Salesforce Docusign package provides out of the box.

Hi ​@kingbak,

Yes, this is possible using the DocuSign Apex Toolkit in Salesforce.

Key Steps to Implement:

1. Create Envelope

  • Use the DocuSign.Envelope class to instantiate a new envelope. This will act as the container for your documents and recipients.
DocuSign.Envelope envelope = new DocuSign.Envelope();
envelope.status = 'created'; // Start in draft mode

2. Determine Unique Recipients

  • Collect all unique signers from your selected Opportunity Documents. Since Bob and Alice may be associated with multiple documents, ensure you deduplicate the list.
Map<String, DocuSign.Recipient> recipientsMap = new Map<String, DocuSign.Recipient>();

for (Opportunity_Document__c doc : selectedDocuments) {
String signerEmail = doc.Signer_Email__c;
String signerName = doc.Signer_Name__c;

if (!recipientsMap.containsKey(signerEmail)) {
DocuSign.Recipient recipient = new DocuSign.Recipient();
recipient.email = signerEmail;
recipient.name = signerName;
recipient.recipientType = 'signer';
recipientsMap.put(signerEmail, recipient);
}
}

3. Generate Documents Using Gen Templates

  • Loop through the selected Opportunity Documents and generate the corresponding documents using the Gen templates. Associate the correct signature tags with the appropriate recipients.
List<DocuSign.Document> envelopeDocuments = new List<DocuSign.Document>();

for (Opportunity_Document__c doc : selectedDocuments) {
// Generate document from Gen template
DocuSign.Document envelopeDoc = new DocuSign.Document();
envelopeDoc.name = doc.Template_Name__c; // Example field containing the template name
envelopeDoc.file = DocuSign.GenUtils.generateDocument(doc.Template_Id__c, doc.Template_Data__c); // Assuming you store template data

// Set signature tabs
DocuSign.SignHereTab signHereTab = new DocuSign.SignHereTab();
signHereTab.recipientId = recipientsMap.get(doc.Signer_Email__c).recipientId; // Link to the recipient
signHereTab.documentId = envelopeDoc.documentId; // Link to the document
envelopeDoc.tabs.add(signHereTab);

envelopeDocuments.add(envelopeDoc);
}
envelope.documents = envelopeDocuments;

4. Add Recipients to Envelope

  • Add all recipients to the envelope.
envelope.recipients = recipientsMap.values();

5. Redirect User to Sender View

  • Use the Apex Toolkit to create the envelope and retrieve the Sender View URL.
DocuSign.EnvelopeService.createEnvelopeResult result = DocuSign.EnvelopeService.createEnvelope(envelope);
String senderViewUrl = DocuSign.ViewService.getSenderView(result.envelopeId, redirectUri);

Redirect the user to senderViewUrl for final review.

Notes:

  1. One Email Per Recipient: By ensuring each signer has a single recipientId across multiple documents, they will receive only one email and can sign all their documents within the same session.
  2. Dynamic Tabs: Use the SignHereTab dynamically to associate each document's signature tag with the appropriate recipient.

What value is in template_data field? Is that pulling from the Gen Template record?


@Islam.Hassanein 


Reply