> 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/s-docs-system-architecture/how-s-docs-generates-documents.md).

# How S-Docs Generates Documents

## How the S-Docs Document Generation Process Works

This workflow outlines how the S-Docs managed package automates document creation and structurally links those new documents back to your existing Salesforce data.

1\. Reading the Source Data (The Inputs) The process kicks off when a user or an automated flow triggers document generation. Before creating anything, the system must first read two critical pieces of existing data:

* The Template (`SDOC__SDTemplate__c`): The engine reads the template settings to understand the formatting, styling, and exactly which data points need to be queried.
* The Base Salesforce Record: The engine queries the specific Salesforce record (e.g., a specific Account, Quote, or Opportunity) to pull the live data required to populate the template.

2\. Merging the Data (The Engine) With the data gathered, the S-Docs Managed Package acts as the processing engine. It takes the live data from your Base Record and injects it into the framework of the Template, rendering the final document behind the scenes.

3\. Generating and Linking Records (The Outputs) To ensure the final document is stored securely and is easy to find, the S-Docs engine executes a series of creations and structural links within the Salesforce database:

* The Document Record (`SDOC__SDoc__c`): The engine creates a primary record to represent the newly generated document.
* The Junction Record (`SDOC__SDRelationship__c`): Because S-Docs are designed to be highly flexible, they do not strictly use master-detail relationships. Instead, the package creates a junction record. This junction creates a permanent "lookup" link between the new Document Record and your original Base Record.
* The File Payload (ContentVersion / Files): Finally, the engine generates the actual physical file (like the PDF or Word document) and pushes it into standard Salesforce Files. It establishes links so that the physical file is visible on both the new Document Record and the original Base Record's "Files" related list.

```mermaid
flowchart TD
    %% Styling classes to make it visually distinct
    classDef input fill:#e1f5fe,stroke:#0288d1,stroke-width:2px;
    classDef process fill:#fff3e0,stroke:#f57c00,stroke-width:2px;
    classDef output fill:#e8f5e9,stroke:#388e3c,stroke-width:2px;

    %% 1. The Inputs
    subgraph Inputs ["1. Source Data"]
        Template["SDOC__SDTemplate__c<br/>(Template Settings)"]:::input
        SFRecord["Base Salesforce Record<br/>(Source Data)"]:::input
    end

    %% 2. The Engine
    Engine(("S-Docs<br/>Managed Package")):::process

    %% 3. The Outputs
    subgraph Outputs ["2. Generated Records"]
        SDoc["SDOC__SDoc__c<br/>(Document Record)"]:::output
        SDRel["SDOC__SDRelationship__c<br/>(Junction)"]:::output
        CV["ContentVersion / Files<br/>(File Payload)"]:::output
    end

    %% Actions (Solid Lines)
    Template -->|Read by| Engine
    SFRecord -->|Read by| Engine
    
    Engine ==>|Generates| SDoc
    Engine ==>|Generates| SDRel
    Engine ==>|Generates| CV

    %% Relationships / Lookups (Dotted Lines)
    SDRel -.->|Lookup to| SDoc
    SDRel -.->|Lookup to| SFRecord
    
    CV -.->|Linked to| SFRecord
```

#### 🎨 Color & Shape Legend

| **Visual Element** | **Element Type**            | **What it Means**                                                                                                       |
| ------------------ | --------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| Blue Rectangles    | Inputs / Source Data        | Existing records or configurations in your Salesforce org that the managed package needs to read before doing its job.  |
| Orange Circle      | The Engine / Process        | The automated action happening behind the scenes. In this case, it is the S-Docs Managed Package processing the inputs. |
| Green Rectangles   | Outputs / Generated Records | The brand new records that are created and saved to your Salesforce database as a result of the engine running.         |

***

#### ➡️ Arrow & Line Legend

| **Line Style**            | **Action Type**       | **What it Means**                                                                                                                                                     |
| ------------------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Thin Solid Arrow (`-->`)  | "Read" Action         | Data is being passed from the Salesforce database into the S-Docs engine to be processed.                                                                             |
| Thick Solid Arrow (`==>`) | "Create" Action       | The S-Docs engine is actively executing a DML Insert to generate these new records.                                                                                   |
| Dotted Arrow (`-.->`)     | Database Relationship | A structural link in Salesforce (like a Lookup, Master-Detail, or a File Link). This shows how the records permanently relate to each other *after* they are created. |


---

# 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 dynamically by asking a question.

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

```
GET https://help.sdocs.com/developer-hub/s-docs-system-architecture/how-s-docs-generates-documents.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
