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
Terence VibanTerence Viban 

EmailTemplate problem

Hi all,
when i modify an existing visualforce email template, the changes are applied on the UI. Now if i query for email template like this 

SELECT id, Body, HtmlValue, Name, DeveloperName, LastUsedDate, CreatedDate, Markup, ApiVersion FROM EmailTemplate WHERE Name = 'QuoteVFTemplate'

the HtmlValue returned is unchanged. However the Markup reflects the changes I made to the template on the UI.

Can someone please tell me why this could be happening or point me to  the right resource? Haven't been able to find any documentation around caching of Email templates.

The use case is, I want to get the HtmlValue of the template and render it on a Visualforce Page as pdf.

Thank you
Best Answer chosen by Terence Viban
Vishnu VaishnavVishnu Vaishnav
Hi,

you can use this to find out htmlbody :

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses('test@gmail.com');      
mail.setTargetObjectId('contactid');//Contact id
mail.setTemplateId(selTemplateId);//Template id
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
system.debug('### Body :'+mail.getHtmlBody()+'=='+mail.getPlainTextBody()); 
Database.rollback(sp);
        

All Answers

Vishnu VaishnavVishnu Vaishnav
Hi,

you can use this to find out htmlbody :

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses('test@gmail.com');      
mail.setTargetObjectId('contactid');//Contact id
mail.setTemplateId(selTemplateId);//Template id
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
system.debug('### Body :'+mail.getHtmlBody()+'=='+mail.getPlainTextBody()); 
Database.rollback(sp);
        
This was selected as the best answer
Terence VibanTerence Viban
Thanks vishnu for your answer. I already had this suggestion from another source, but couldn't imagine that I really had to fake sending an email to be able to get the html of the template. I saw two unanswered question dating back to 2008 and 2009 which shows that this issue has existed for quite a long time. This means the HtmlValue of the template is not very useful. 
Once more thanks for your suggestion