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

Send email page with template in Visualforce

I would like to create a similar page to the standard one for sending emails in Visualforce. The idea is to be able to pre-propulate the email page with the template defined for Solution and capture the Send action to change Case status to "Solution provided to customer". I don't know how to read the template and show it with the merge fields replaced by their value, is there a standard way to do this?
Ron HessRon Hess
no good way to read a template and "evaluate" all the merge fields, however thre is a way to build and send email , change values / status in Visualforce/Apex. 

you would have to begin by building your final template in Visualforce, rather than in a template, let's call this /apex/emailTemplate, in the page attribute specify the output format as html, or pdf, this page takes an id paramater and fills in the expressions (perhaps a standard controller here)

next, construct another page that will fetch the first page(/apex/emailTemplate) with the case ID, then performs (in apex controller) page.getContent();

here is a snippet , notice we create a page, just to get it's body as a string, it never shows in the browser.
the body can be send in an email, the fields will be filled in by the Visualforce page generator ( getContent() ), pretty cool feature if you ask me

        // Pass in the endpoint to be used using the string url
        PageReference pageRef = new PageReference( getVisualforcePageName() +'?id=' + record_id );
        system.assert(pageRef != null);
        // run the requested page thru the visualforce page generator now
        pageBody = pageRef.getContent();
        system.assert(pageBody != null && pageBody != '');
        return pageBody;

now you can use that content with the email services of apex, to send the email, and finaly change the status.

you lose the ability to tack on comments or change the template by the end user, but you coudl add some standard note fields on the case and get something like this back again.
Thanks Ron, it's a good idea, but I've found the following drawback: the user has to review the email before sending it and be able to modify or add some lines. I've been trying to show the email body in the Apex page that gets the content of the other Apex email page in an inputTextArea but html tags appear and accents are not shown properly, I can't find a solution to show it and let the user the possibility to modify it!
Hi Ron,
I have been trying this option as it suits our business process perfectly. But I am facing some challenge when I try to get the page content from within a class. I have an apex method say methodA that creates a new visualforce page and gets the content from another visualforce page, excatly as mentioned in this thread by you. The only difference is I am invoking metodA from another apex class. When I do that, the the getContent() method return a blank page. Any ideas?

pageBody = pageRef.getContent().toString()
The above works fine when I invoke the methodA from javascript.
I would greatly appreciate if you can please provide some insight to this. Also please let me know if you need any other information to make it more clear.
Thanks and regards,

Message Edited by arasu on 08-11-2008 10:04 AM