# Apex Method Reference: Document and Email Generation

This reference guide details the core synchronous methods available in the `SDOC.DocumentSDK` class for generating documents and emails.

> Crucial Context Limitation: All methods listed below are static. They can be called from Invocable variables (Flows), Queueables, Batches, `@future` methods, and Aura/Visualforce controllers. They cannot be called directly from an Apex Trigger.

***

### Document Generation Methods

These methods allow you to generate S-Docs programmatically. Whenever possible, use the modern `generateDocument` method, as it supports the latest features like Runtime Prompts and advanced file saving options.

### 1. `generateDocument` (Modern)

This is the primary, recommended SDK method for generating PDF and PDF-UPLOAD formats.

Available: S-Docs v7.1+ (May 2024 and later)

Returns: `String` (A JSON representation of the generated document, including `id`, `contentDocumentId`, `attachmentName`, and `createdDate`).

Apex

```
generateDocument(Id templateId, Id recordId, SDOC.GenerationOptions genOptions)
```

| **Parameter** | **Type**                 | **Description**                                                                                                 |
| ------------- | ------------------------ | --------------------------------------------------------------------------------------------------------------- |
| `templateId`  | `Id`                     | The Salesforce ID of the S-Docs template.                                                                       |
| `recordId`    | `Id`                     | The Salesforce ID of the base record (e.g., Account, Opportunity).                                              |
| `genOptions`  | `SDOC.GenerationOptions` | An object containing sub-options for user inputs (`documentOptions`) and email configurations (`emailOptions`). |

*(Note: The older two-parameter version of this method, `generateDocument(Id templateId, Id recordId)`, was deprecated in v7.1).*

### 2. `generateDoc` (Legacy)

This is a legacy method used primarily for older PDF generation engines or MSX documents. It returns the S-Doc associated with the generated document as a JSON String.

Apex

```
generateDoc(Id templateId, Id recordId)
// OR with sharing enforcement:
generateDoc(Id templateId, Id recordId, Boolean enforceSharingRules)
```

| **Parameter**         | **Type**  | **Description**                                                     |
| --------------------- | --------- | ------------------------------------------------------------------- |
| `templateId`          | `Id`      | The Salesforce ID of the S-Docs template.                           |
| `recordId`            | `Id`      | The Salesforce ID of the base record.                               |
| `enforceSharingRules` | `Boolean` | Pass `true` to enforce Salesforce sharing rules, `false` to bypass. |

### 3. `createSDoc` (Legacy)

An older method that returns an actual `SDoc__c` SObject record instead of a JSON string.

Apex

```
createSDoc(Id templateId, Id recordId, SDOC.DocumentOptions documentOptions)
```

***

### Email Generation Methods

Because S-Docs email templates can leverage related lists, conditional logic, and direct SOQL queries, you can use the SDK to generate much richer email HTML than standard Salesforce email templates allow.

### 1. `generateEmail` (HTML Body Only)

Use this method when you simply need S-Docs to construct the HTML email body, but you plan to use standard Salesforce Apex (`Messaging.SingleEmailMessage`) to handle the actual sending and routing.

Returns: `String` (The compiled HTML email body).

Apex

```
generateEmail(Id templateId, Id objectId)
```

| **Parameter** | **Type** | **Description**                                 |
| ------------- | -------- | ----------------------------------------------- |
| `templateId`  | `Id`     | The Salesforce ID of the S-Docs Email template. |
| `objectId`    | `Id`     | The Salesforce ID of the base record.           |

### 2. `generateEmail` (With Routing Options)

Use this method when you want S-Docs to handle the routing details (To, CC, BCC, Reply-To, Org-Wide Addresses) and prep the email for sending.

Returns: `String` (A JSON string containing a serialized `Messaging.SingleEmailMessage` object).

Apex

```
generateEmail(Id templateId, Id objectId, SDOC.EmailOptions emailOptions)
```

| **Parameter**  | **Type**            | **Description**                                                                                                              |
| -------------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `emailOptions` | `SDOC.EmailOptions` | An object allowing you to pass lists of `toAddresses`, `ccAddresses`, `replyToEmail`, `emailSubject`, and `sdocIdsToAttach`. |

### 3. `generateEmail` (With Attachments)

Use this method when you need to quickly generate an email and automatically attach previously generated S-Docs (or send them as S-Sign requests).

Returns: `String` (A JSON string containing a serialized `Messaging.SingleEmailMessage` object).

Apex

```
generateEmail(Id templateId, Id objectId, List<Id> sdocIdsToAttach, Boolean enforceSharingRules)
```

| **Parameter**         | **Type**   | **Description**                                                     |
| --------------------- | ---------- | ------------------------------------------------------------------- |
| `sdocIdsToAttach`     | `List<Id>` | A list of `SDoc__c` record IDs to attach to the outbound email.     |
| `enforceSharingRules` | `Boolean`  | Pass `true` to enforce Salesforce sharing rules, `false` to bypass. |


---

# 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/apex-method-reference-document-and-email-generation.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.
