# Developer SDK: Automation & Programmatic Generation

#### Why use the S-Docs SDK?

The S-Docs SDK is the engine for "headless" document automation. While the standard managed package provides a robust UI for manual generation, the SDK allows developers to bypass the interface and embed document logic directly into custom LWCs, complex Apex processes, or automated backend flows.

#### **What is the SDK?**

The SDK is a collection of static Apex methods (residing in the `SDOC` namespace) that provide granular control over the document lifecycle. It is often used in tandem with, or as a replacement for, our front-end components:

* The **LWC (Front-End)**: Handles user interaction, template selection, and manual "click" events on record pages.
* The **SDK (Back-End)**: Handles the programmatic building, merging, and distribution of files.

#### Solve: Common Implementation Patterns

* LWC Integration: Calling `SDOC.DocumentSDK.generateDocument` from a custom controller to return a file ID to the UI.
* Headless Automation: Triggering generation via a Flow Invocable Action when a record reaches a specific status.
* Bulk Merging: Using `generateDocsInBatch` to process hundreds of records into a single, merged PDF.

#### Technical Reference & Implementation Logic

The SDK is designed to be lightweight and compatible with standard Salesforce Apex patterns.

* Global Access: All methods are static. Invoke them directly using `SDOC.DocumentSDK.methodName()`.
* Supported Contexts: Invocable Actions, Queueable Apex, Batch Apex, `@future` methods, and Aura/LWC Controllers.
* Critical Restriction: Synchronous SDK calls are not supported in Apex Trigger context. To trigger generation from a database change, you must use Platform Events or an asynchronous wrapper (Queueable/`@future`).

#### Choosing Your Processing Mode

When designing your solution, use the table below to determine if you should leverage the Synchronous SDK or the Asynchronous Jobs engine.

|                        |                                                                          |                                                                  |
| ---------------------- | ------------------------------------------------------------------------ | ---------------------------------------------------------------- |
| **Feature**            | **Synchronous (SDK)**                                                    | **Asynchronous (S-Docs Jobs)**                                   |
| **Primary Use**        | Real-time UI (LWC/Buttons) and Single-file logic.                        | Bulk processing and "Set it and forget it" automation.           |
| **Speed**              | **Immediate**. Returns metadata/IDs in the same transaction.             | **Variable**. Dependent on the Salesforce Org's Apex flex queue. |
| **Transaction Limits** | **High Risk**. Shared with your calling code’s SOQL/CPU limits.          | **Low Risk**. Runs in its own dedicated transaction.             |
| **Trigger Support**    | **Prohibited**. Will throw an Exception if called directly in a Trigger. | **Fully Supported**. Designed to be "fired" from a Trigger.      |
| **Hard Limitations**   | Supports **PDF/PDF-UPLOAD only**. Max 1 call per transaction.            | Supports **All Formats**. Best for heavy DOCX/XLSX generation.   |

### SDK Method Directory

*Select a method below to view technical signatures and code examples*

#### [Document Generation](/developer-hub/document-generation-workflows/apex-and-programmatic-automation/software-development-kit/document-generation.md)

* [generateDocument (Modern)](/developer-hub/document-generation-workflows/apex-and-programmatic-automation/software-development-kit/document-generation/method-generatedocsinbatch.md): The primary method for synchronous PDF and PDF-Upload generation.
* [generateDocsInBatch](/developer-hub/document-generation-workflows/apex-and-programmatic-automation/software-development-kit/document-generation/method-generatedocsinbatch.md): Initiates an asynchronous batch job for multiple records.
* generateDoc (Legacy): Supported for older engines and MSX documents.

#### Email & Management

* [generateEmail](/developer-hub/document-generation-workflows/apex-and-programmatic-automation/software-development-kit/document-generation/method-generateemail.md): Compiles S-Docs email templates into HTML bodies or serialized email objects.
* combineDocuments: Merges multiple existing S-Doc IDs into a single PDF file.
* refreshDocumentData: Updates S-Doc metadata based on recent base-record changes.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.sdocs.com/developer-hub/references/software-development-kit-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
