function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Replace merge fields in EmailTemplate-body (string) manually in apex?

I have a visualforce page with a controller.
Two parameters are passed: the ID of a custom-object and the ID of an EmailTemplate-Object.
I query the EmailTemplate and want to output the template in a textarea. That works fine.
Unfortunately the merge fields in the EmailTemplate are as they are in the template (e.g.  "{!Custom_Object__c.Field1}").
Is there are simple way to replace those placeholders with the values of the custom-object I get from the passed ID?

I tried this:
But unfortunately it does not work because I cannot run Database.setSavepoint() at that point. I get the error "DML currently not allowed".

Is there another way to accomplish what I want to do without doing a simple string-replace for all possible Fields of Custom_Object__c?
James (CloudAnswers)James (CloudAnswers)
I know this thread is old, but in case the next person from google finds it helpful...

I was looking for a solution to the same thing but coulnd't find it, so I made an apex class to do the search/replace like the templates do: