[SalesForce] Preview Email template within Visualforce page, some throw an error

I have a custom page to preview and render Email templates,
now i came across that some (not many) templates will exceed the view state of the page ( max 137kb)
As I need the function to replace text within the template the displayed variable (transient) needs to be at some point be reassigned with a new value, but as soon i do that in the code (touching the value of that var again by assigning a new string the page drops out with the view state exceeded error (already on loading the page after selecting the template) if I don't "touch" the variable all is fine
here is how i show the preview.

<apex:pageBlockSection title="Preview" columns="1"> 
            <apex:outputText value="{!theTempl}" escape="false" />
</apex:pageBlockSection>  

some other templates on the other hand work fine but one of them throws an error
STRING_TOO_LONG, HTML Body: data value too large:
As soon it goes to be assembled in a complete email
But I could not find any limits for the EmailTemplate htmlValue

1) Does anyone got an idea how to avoid this view state thing, as this is only on some templates
2) How can I avoid this String too Long error, when all other actions ( string replace) went fine and only next stop is to send out the email

Thanks

Best Answer

I would refer you to the following docs/posts related to view state to help you troubleshoot the problems you're having with your templates. In some case, its a matter of needing to define your variables as transient to reduce the view state. It can also be one of needing to reduce the number of forms on the page, accessing too many objects at once, or simply needing to optimize your queries so you return only the info that's needed. Here's a list of docs and posts I think you'll find the most helpful to you:

Related Topic