> For the complete documentation index, see [llms.txt](https://help.sdocs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.sdocs.com/developer-hub/document-workflows/software-development-kit/document-generation/method-generatedocument-modern.md).

# 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.

```java
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:

```json
{
  "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.

```java
@AuraEnabled
public static String getNewDocument(Id targetId, Id tempId) {
    // Generate and parse the result
    String jsonResp = SDOC.DocumentSDK.generateDocument(tempId, targetId, null);
    Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(jsonResp);
    
    return (String)data.get('contentDocumentId');
}
```

**Scenario B: Generation with Runtime User Inputs**

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

```java
SDOC.GenerationOptions genOptions = new SDOC.GenerationOptions();
Map<String, String> inputs = new Map<String, String>{
    'Custom_Note' => 'This is a special instruction for this PDF.'
};
genOptions.documentOptions = new SDOC.DocumentOptions(inputs);

String result = SDOC.DocumentSDK.generateDocument(templateId, recordId, genOptions);
```

***

### ⚠️ 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.sdocs.com/developer-hub/document-workflows/software-development-kit/document-generation/method-generatedocument-modern.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
