# Fixing Template Errors After Template Migration

### ![thinking face](https://pf-emoji-service.prod-east.frontend.public.atl-paas.net/assets/standard/ef8b0642-7523-4e13-9fd3-01b65648acf6/32x32/1f914.png) Problem <a href="#ud83e-udd14-problem" id="ud83e-udd14-problem"></a>

You or a client just used the standard migration method under S-Docs Setup to migrate templates from one org to another. Afterwards, most or all of those templates trigger an error upon use until you go to that template and manually re-save it.

### ![seedling](https://pf-emoji-service.prod-east.frontend.public.atl-paas.net/assets/standard/ef8b0642-7523-4e13-9fd3-01b65648acf6/32x32/1f331.png) Solution <a href="#ud83c-udf31-solution" id="ud83c-udf31-solution"></a>

{% stepper %}
{% step %}

### Step: Verify templates have errors

Start by verifying the field `SDOC__Has_Error__c` is set to true for those templates. Spot checking only a couple should be fine. To get a full list, run the following query in Salesforce Inspector:

```sql
SELECT Id FROM SDOC__SDTemplate__c WHERE SDOC__Has_Error__c = true
```

{% endstep %}

{% step %}

### Step: Add Apex class to toggle the field

Add the following Apex class to auto-toggle that field from `TRUE` to `FALSE`:

```java
public class HasErrorToggler {
    public static void toggleBooleanField(List<SObject> records, String fieldName) {
        if (records == null || records.isEmpty()) return;

        for (SObject rec : records) {
            Boolean currentValue = (Boolean) rec.get(fieldName);
            rec.put(fieldName, currentValue == null ? true : !currentValue);
        }
        update records;
    }
}
```

{% endstep %}

{% step %}

### Step: Run the Apex class

Then, run a simple command in the Developer Console to run the Apex class:

```java
List<SDOC__SDTemplate__c> temps = [
    SELECT Id, SDOC__Has_Error__c
    FROM SDOC__SDTemplate__c
    WHERE SDOC__Has_Error__c = true
];

HasErrorToggler.toggleBooleanField(temps, 'SDOC__Has_Error__c');
```

{% hint style="info" %}
By adding a `LIMIT 1` to the query, you can verify if this is working. You’ll see the record count in Salesforce Inspector drop by 1.
{% endhint %}
{% endstep %}

{% step %}

### Step: Test generation

Finally, try to generate a document with a couple of the templates that previously errored. They should generate.

{% hint style="info" %}
Keep in mind, some templates may still trigger that same error. This means there is something else keeping it from toggling, such as an invalid ID in the preview ID field, or it references a component that no longer exists, etc.
{% endhint %}
{% endstep %}
{% endstepper %}


---

# 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/sdocs/administration/landing-page-for-admin/template-migration/fixing-template-errors-after-template-migration.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.
