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
Ron WildRon 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?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

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}.

Message Edited by sfdcfox on 12-28-2009 11:06 PM

All Answers

Ron WildRon Wild

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,

nelloCnelloC
Ron, did you find a solution to this? I'm trying to replace cr/lf within a template body with '<br/>' to render on a visualforce page. The string replace method doesn't seem to find the \r\n's.
nelloCnelloC

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;
}

 

 Oops...!! just realised a string can contain cr/lr or lf. Revised code above.
Message Edited by nelloC on 12-01-2009 09:07 AM
Ron WildRon Wild

Can we call Apex methods from Visualforce email templates now?  That would be great news if we can.

 

Ron

nelloCnelloC
I've no experience of Visualforce email templates so I don't know the answer to that one...
TehNrdTehNrd
The workaround to using Apex in visualforce templates is to create a component with a custom controller and then place that component in the Visualforce email template.
Ron WildRon Wild
That's very useful information.  Thanks!
TedLiuTedLiu

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

sfdcfoxsfdcfox

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}.

Message Edited by sfdcfox on 12-28-2009 11:06 PM
This was selected as the best answer