You need to sign in to do that
Don't have an account?
Tolga Sunar
Visualforce - Standardcontroller components are not rendering when called from JS
I am trying to preview the email template inside a Visualforce page, and for this purpose, I query for the EmailTemplate record's body inside JavaScript, and then I let the standardcontroller do rest of the job by merging the referenced fields in email template - at least that's my intention.
The email template body is:
And the rendered VF page is:
What am I doing wrong? It seems like the {!Flight__c...} statements need to be present as hardcoded inside VF for standardcontroller to merge them. Maybe I should follow a different path there, looking forward to your insights.
Thanks in advance.
The email template body is:
Flight No: {!Flight__c.Name}The visualforce page is (embedded into a publisher action):
<apex:page standardController="Flight__c"> <apex:includeScript value="/soap/ajax/35.0/connection.js"/> <apex:form id="myForm"> <apex:pageBlock id="myPageblock"> <apex:outputText> <script type="text/javascript"> sforce.connection.sessionId='{!GETSESSIONID()}'; var query = sforce.connection.query("SELECT Id, Name, Body FROM EmailTemplate WHERE Name = 'myTemplate'"); var records = query.getArray("records"); alert(records); var body = ''; body = records[0].Body; document.write(body); </script> </apex:outputText><br/> <apex:outputText> <script> document.write('Flight No: {!Flight__c.Name}'); </script> </apex:outputText> </apex:pageBlock> </apex:form> </apex:page>
And the rendered VF page is:
What am I doing wrong? It seems like the {!Flight__c...} statements need to be present as hardcoded inside VF for standardcontroller to merge them. Maybe I should follow a different path there, looking forward to your insights.
Thanks in advance.
This is correct behavior.
- Your first line in the above shows that when you try to output the Body of an EmailTemplate, the merge fields are still literal text.
- Your second line shows that you can use merge fields in Javascript.
It is probably more straightforward to do a manual merge in Apex. The regex approach isn't terribly complicated, though if you want to support cross-object fields or you merge in data from more than one object, you need to get more fancy.Here is a basic outline of the steps you can take:
Getting the merge fields : Helpers : Putting it all together : Using it in an extension : Please mark this post as solved so that it gets removed from the unanswered queue which results in helping others who are really in need of it.
Regards,
Nagendra.P