Hello Eliot,
You're right, directly calling the DocuSign API from Apex to generate a document without the interactive DocuSign UI requires a different approach than using the dfsle__gendocumentgenerator Visualforce page.
Here's a breakdown of the recommended approach and the steps involved:
Since you don't need the signing process, you'll primarily interact with the DocuSign eSignature REST API to generate the document based on your template.
Steps:
-
Authentication with DocuSign:
- You'll need to establish secure authentication with your DocuSign account. The recommended method for server-to-server integration like this is using JWT (JSON Web Tokens) Grant authentication.
- This involves:
- Creating an Integration Key in your DocuSign developer account.
- Generating a private/public key pair.
- Requesting an access token from DocuSign by signing a JWT containing your Integration Key, user ID, and the private key.
- Caching the access token and refreshing it as needed.
- you can check this link to learn more about Auth with Docusign: https://developers.docusign.com/platform/auth/
-
Identify the DocuSign Template:
- You already have the Template ID (
a1OAZ0000007wXM2AY). You'll use this to tell DocuSign which template to use for generation.
-
Prepare Data for the Template:
- Your DocuSign template likely has roles and fields that need to be populated with data from your Salesforce Opportunity.
- You'll need to query the necessary fields from the
Opportunity object in Apex. - Construct a JSON payload containing the data that maps to the roles and fields in your DocuSign template.
-
Call the DocuSign API to Create an Envelope:
- Use the DocuSign eSignature REST API endpoint for creating envelopes (
/restapi/v2.1/accounts/{accountId}/envelopes). Replace {accountId} with your DocuSign account ID. - In the request body, you'll specify:
documents: An array defining the document to be generated. You'll reference your template here. templateId: Your DocuSign template ID. templateRoles: An array mapping Salesforce data to the roles and fields in your template. This is where you'll pass the data you prepared in step 3. - Crucially, to only generate the document without initiating a signing process, you'll need to ensure there are no recipients defined in a signing role within the envelope definition. You might need to configure your template accordingly or use a role that doesn't trigger signing.
-
Retrieve the Generated Document:
- After successfully creating the envelope, you'll need to make another API call to retrieve the generated document.
- Use the Get Envelope Documents endpoint (
/restapi/v2.1/accounts/{accountId}/envelopes/{envelopeId}/documents). Replace {envelopeId} with the ID of the envelope you just created. - You can specify the format of the document you want to retrieve (e.g.,
application/pdf).
-
Store the Document in Salesforce:
- Once you have the document data (as a Blob in Apex), you can create a
ContentVersion object in Salesforce to store it as a file related to your Opportunity. - Set the
Title, PathOnClient, and VersionData fields of the ContentVersion. - Link the
ContentVersion to the Opportunity using a ContentDocumentLink object.
Apex Implementation Considerations:
- HTTP Callouts: You'll need to use the
HttpRequest and HttpResponse classes in Apex to make calls to the DocuSign API. - JSON Handling: Use the
JSON.serialize and JSON.deserializeUntyped methods to work with the JSON payloads for the API requests and responses. - Error Handling: Implement robust error handling to catch exceptions during API calls and Salesforce data operations.
- Authentication Management: Implement logic to securely store and manage your DocuSign API credentials and access tokens. Consider using custom settings or a secure storage mechanism.
Thank you so much and if you need any help you can raise a support case anytime at https://support.docusign.com