# Solve: Monitoring Batch Status & Handling Errors

### Why?

Because `generateDocsInBatch` runs asynchronously, the SDK returns a Job ID immediately, but the documents aren't created instantly. To build a robust integration, you need to know when the job finishes, if it failed, and why.

### What: The Async Engine

When you call the Batch SDK, S-Docs inserts a record into the standard Salesforce Apex Flex Queue. You can monitor this process either manually through the UI or programmatically via SOQL.

***

### Solve: Implementation Patterns

**1. Manual Monitoring (Admin View)**

The quickest way to check a job's progress:

1. Navigate to Setup > Apex Jobs.
2. Look for the Job ID returned by the SDK.
3. Check the Status (Queued, Preparing, Processing, Completed, or Failed).
4. If the status is "Failed," check the Status Detail column for the specific Governor Limit hit (e.g., *Too many SOQL queries*).

**2. Programmatic Monitoring (Developer View)**

If you are building a custom UI (like a "Processing..." spinner in an LWC), you can query the `AsyncApexJob` object using the ID returned by the SDK.

Java

```
// Query the status of your specific Batch Job
AsyncApexJob jobInfo = [SELECT Status, NumberOfErrors, JobItemsProcessed, TotalJobItems 
                        FROM AsyncApexJob 
                        WHERE Id = :batchJobId];

System.debug('Job Status: ' + jobInfo.Status);
```

**3. Handling Common Batch Errors**

If your batch job fails, it is usually due to one of the following "Big Three" Salesforce limits. Use these strategies to solve them:

| **Error Message**             | **Why it happened**                                       | **The Solve**                                                                            |
| ----------------------------- | --------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| Too many SOQL queries: 201    | The template is too complex for the current batch size.   | Reduce the `batchSize` parameter in your SDK call (try 1 or 2).                          |
| Apex heap size too large      | The resulting PDFs or data sets are too large for memory. | Reduce the total number of records in your `recordIds` list.                             |
| Too many queueable jobs added | You are trying to chain too many async processes at once. | Ensure Document Actions are disabled on the template; they are unsupported in Batch SDK. |

***

### Pattern: Post-Batch Logic

If you need to perform an action *after* the batch finishes (like updating a checkbox on the base records), the Batch SDK does not currently support a native "onComplete" callback.

The Recommended Solve:

Use a Scheduled Flow or a Scheduled Apex class that runs every hour to check for S-Doc records created within that window and perform your follow-up logic.


---

# 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/document-generation-workflows/apex-and-programmatic-automation/software-development-kit/implementation-solves/solve-monitoring-batch-status-and-handling-errors.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.
