You need to sign in to do that
Don't have an account?
Ron Wild
Visualforce Email - Function to replace linefeed with <br/>?
I'm trying to format an address field for display in a VF email template and haven't found a way to replace line feeds/carriage return with an html tag.
I'd like to do it within the template, but if I have to create a formula field and merge that field I will.
SUBSTITUTE( PDS__Shipping_Address__c ,BR(),'<br/>') .... doesn't seem to find the line feeds.
SUBSTITUTE( PDS__Shipping_Address__c ,CHR(13),'<br/>') .... rejects CHR() as an unknown function.
Besides writing a trigger to do this in Apex, is there another solution?
The CR/LF characters are placed in the output stream (you can verify this using View Source), but because HTML normally uses the CSS style "white-space: normal" (known as "collapse mode"), they vaporize in the rendering. The simple solution is to add a CSS style using "white-space: pre" ("pre-formatted mode") or "white-space: pre-line" (collapse tabs and spaces, but preserve CR/LF characters). No need to use fancy String.Replace, String.indexOf, or any other complex code. The difference between "pre" and "pre-line" is that extra spaces inside the text are preserved when using "pre", while "pre-line" only takes into consideration end-of-line characters.
Edit: Also, the message above is somewhat out of context to this thread, but the solution is to use a merge field. The context for any merge field in a template is {!Object.Field}, so the standard Case field would be {!Case.Thread_Id}, and a custom one might be {!Custom_Order__c.Thread_Id__c}.
All Answers
This is a bit of an issue for one of my clients, and I'm really hoping I don't have to write an apex trigger for each object and field that has linefeeds in order to display them in a visualforce email template with <br/> tags.
Are there any salesforce techies watching this thread that can help?
I need a SUBSTITUTE( field__c, BR(), '<br/>') type function that works...
Thanks,
In case anyone else comes across this problem... the string function replace doesn't seem to identify line feeds but oddly enough indexOf does. So one solution I've come up with is this:
public static string LineFeedToHTMLBreaks(string stringIn)
{
string LF = '\n';
string CR_LF = '\r\n';
string HTML_BREAK = '<BR/>';
string searchArg = null;
integer startPos = 0;
integer pos;
// Determine whether cr/lf or just lf in this string
pos = stringIn.indexOf(CR_LF, startPos);
if (pos != -1)
searchArg = CR_LF;
else {
pos = stringIn.indexOf(LF, startPos);
if (pos != -1)
searchArg = LF;
}
// Replace line feeds if found in string
if (searchArg != null)
{
do {
pos = stringIn.indexOf(searchArg, startPos);
if (pos != -1)
{
string newString;
if (stringIn.length() > pos + searchArg.length())
newString = stringIn.substring(0, pos) + HTML_BREAK + stringIn.substring(pos + searchArg.length());
else
newString = stringIn.substring(0, pos) + HTML_BREAK;
stringIn = newString;
startPos = pos + HTML_BREAK.length();
}
} while (pos != -1 && pos < stringIn.length());
}
return stringIn;
}
Can we call Apex methods from Visualforce email templates now? That would be great news if we can.
Ron
hi,
do you know how to place/put the component in the subject line of visualforce email template.
I wrote a small function to generate Thread_Id in my apex class and now i want this Thread_Id in the "Subject" line of VF email template.
Any light will be appreciated.
Thanks
The CR/LF characters are placed in the output stream (you can verify this using View Source), but because HTML normally uses the CSS style "white-space: normal" (known as "collapse mode"), they vaporize in the rendering. The simple solution is to add a CSS style using "white-space: pre" ("pre-formatted mode") or "white-space: pre-line" (collapse tabs and spaces, but preserve CR/LF characters). No need to use fancy String.Replace, String.indexOf, or any other complex code. The difference between "pre" and "pre-line" is that extra spaces inside the text are preserved when using "pre", while "pre-line" only takes into consideration end-of-line characters.
Edit: Also, the message above is somewhat out of context to this thread, but the solution is to use a merge field. The context for any merge field in a template is {!Object.Field}, so the standard Case field would be {!Case.Thread_Id}, and a custom one might be {!Custom_Order__c.Thread_Id__c}.