For the complete documentation index, see llms.txt. This page is also available as Markdown.

Method: generateDocument (Modern)

Why?

Use generateDocument when your application requires immediate document generation and metadata return. This is the primary tool for "Real-Time" document needs, such as showing a PDF preview in an LWC immediately after a user clicks a button, or getting a ContentDocumentId to attach to a specific record in the same transaction.

What: Technical Signature

This method is synchronous and resides in the SDOC.DocumentSDK class.

public static String generateDocument(Id templateId, Id recordId, SDOC.GenerationOptions options)
Parameter
Type
Required
Description

templateId

Id

Yes

The 18-character Salesforce ID of the S-Docs Template.

recordId

Id

Yes

The ID of the base record (e.g., Account, Opportunity).

options

Object

No

An instance of SDOC.GenerationOptions for advanced config (Email, User Inputs, etc.).

Return Value Returns a JSON String. To access the data, deserialize it into a Map or a typed class:

{
  "id": "a00...", 
  "contentDocumentId": "069...", 
  "attachmentName": "Invoice_V1.pdf",
  "createdDate": "2024-05-20",
  "ssignEnabled": "false"
}

Solve: Implementation Patterns

Scenario A: Basic LWC Controller

Return the file ID directly to your front-end component for immediate viewing.

Scenario B: Generation with Runtime User Inputs

Inject data gathered from a custom UI directly into the document's merge fields.


⚠️ Constraints & Limitations

  • Format Support: Optimized for PDF and PDF-UPLOAD formats only.

  • Context: Cannot be called directly within an Apex Trigger. Attempting to do so will throw a GenerationException.

  • Transaction Limit: To protect org performance and governor limits, this method is restricted to one invocation per Apex transaction.

  • Heap Size: Large templates with complex related lists may consume significant heap size. If you encounter Heap Limit Exceeded, consider offloading to the Asynchronous Jobs engine.

Last updated

Was this helpful?