Skip to main content
Question

Generate contract (pdf) from Salesforce Opportunity (Apex)

  • April 15, 2025
  • 1 reply
  • 13 views

Forum|alt.badge.img

Hello all,

We have a button on our Salesforce Opportunities that redirects us to a standard Docusign page to generate the contract and get it into Salesforce files.

Our button calls this url, providing the template id:

{!URLFOR('/apex/dfsle__gendocumentgenerator', null, [sId = Opportunity.Id, templateId = 'a1OAZ0000007wXM2AY', recordId = Opportunity.Id, recordName = Opportunity.Name, title = 'Contrat'])}

Once clicked, the contract is being generated and when we get back on our Opportunity in Salesforce, we can find the file related (pdf).

 

Our need is to replace that manual action using apex by calling Docusign API to generate and store it as file on the Opportunity.

I have checked on forum, but did not find an easy to integrate solution, what would you recommend please?

NB: We dont need to sign the contract, just to generate it

 

Thanks for your help!

 

1 reply

Forum|alt.badge.img+3
  • Docusign Employee
  • 24 replies
  • April 16, 2025

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:

  1. 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/
  2. Identify the DocuSign Template:

    • You already have the Template ID (a1OAZ0000007wXM2AY). You'll use this to tell DocuSign which template to use for generation.
  3. 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.
  4. 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.
  5. 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).
  6. 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