1

I have an email template which has values of two merge fields of type "Rich Text Area(32000)". Below is the requirement:

1) In my email template I want to restrict this value to only 200 characters.

2) I want to indent the rows properly so that they are easily readable.

Below is the V.f template code:

<messaging:emailTemplate relatedToType="Idea" 
subject="({!relatedTo.Categories})  ({!relatedTo.Product__c}) : New Discussion Posted">
<messaging:htmlEmailBody >
    <html>
        <body>
         <STYLE type="text/css">
            TH {font-size: 11px; font-face: arial}
            TD  {font-size: 11px; font-face: arial}
            P  {font-size: 11px; font-face: arial}
         </STYLE>
        <p><font face="verdana" size="2"><i><b>New Discussion</b></i></font></p>
        <table border="0" >
            <tr>
                <td><font face="verdana" size="2"><i><b>{!relatedTo.Title}</b></i><br/><br/></font>
                </td>
            </tr>
            <tr>
                <td><font face="verdana" size="2" color="#00CCFF"><apex:outputText value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Description:" escape="false"/></font><font face="verdana" size="2"><apex:outputText value="&nbsp;{!relatedTo.Body}" escape="false"/><br/><br/></font>
                </td>
            </tr>
            <tr>
                <td><font face="verdana" size="2" color="#00CCFF"><apex:outputText value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Latest Comment:" escape="false"/></font><font face="verdana" size="2"><apex:outputText value="&nbsp;{!relatedTo.Last_Comment__c}" escape="false"/><br/><br/></font>
                </td>
            </tr>
        </table>
        <p/><br/><br/>
        <Left><font face="verdana" size="2">
            <apex:outputLink value="{!relatedTo.BaseURL__c}/{!relatedTo.Id}">
                For more detailed information join the discussion...
            </apex:outputLink></font>
        </Left>
        </body>
    </html>
</messaging:htmlEmailBody>

The fields Last_Comment__c and Body are of RichText type.

enter image description here

In the above Email screenshot we can see that the 2nd row of the description field starts from extreme left . I want it to start from where the first row has started. also the first line spans to a huge length. Can we wrap its text somehow ?

I want it something like below.

enter image description here

Please help me with this.

For BOB:

Below is the screenshot of what I have actually in my fields

enter image description here

Below is what I am getting in email

enter image description here

Here is the updated code:

<messaging:emailTemplate relatedToType="Idea" 
subject="({!relatedTo.Categories})  ({!relatedTo.Product__c}) : New Discussion Posted">
<messaging:htmlEmailBody >
<html>
<head>
<style>
  body {
  font-size:.8em;
  font-family: Arial
  }

.title {
 color:#00CCFF;
 width:20%;
 display:inline-block;
 vertical-align:top;
}

  .body {
  width:80%;
  display:inline-block;
  margin-bottom:1em;
 }
</style>
</head>
<body>

<p><b><i>New Discussion</i></b></p>
 <p><i><b>{!relatedTo.Title}</b></i><br/></p>

   <span class="title">Description:</span>
   <span class="body">{!relatedTo.Body}<br/><br/></span>

   <span class="title">Latest Comment:</span>
   <span class="body">{!relatedTo.Last_Comment__c}<br/><br/></span>

   <font face="verdana" size="2">
    <apex:outputLink value="{!relatedTo.BaseURL__c}/{!relatedTo.Id}">
      For more detailed information join the discussion...
    </apex:outputLink>
   </font>

</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

Thanks!

Ruchi

1

Couple of suggestions:

  • Don't use tables for layout.
  • You're using both inline and segregated CSS. This is inconsistent. Move your inline CSS inside the <style> block.
  • There's no such thing as a <left> element. If you want to align left, use CSS.
  • That's not how you use font-face. See here for how. You probably mean to use font-family.
  • Don't use <font> elements to set fonts - use CSS instead.

Here's a version of your code without tables.

HTML:

<!DOCTYPE html>
<html>
<body>
  <p>&lt;New Discussion, New Comment or Discussion Closed&gt;</p>
  <p>&lt;Text of the Original Question&gt;</p>

  <span class="title">Description:</span>
  <span class="body"><apex:outputText value="{!relatedTo.Body}" escape="false"/></span>

  <span class="title">Latest Comment:</span>
  <span class="body">{!relatedTo.Last_Comment__c}</span>

</body>
</html>

CSS:

body {
  font-size:.8em;
  font-family: Arial
}

.title {
  color:lightblue;
  width:20%;
  display:inline-block;
  vertical-align:top;
}

.body {
  width:80%;
  display:inline-block;
  margin-bottom:1em;
}

Here's a demo:

enter image description here

Note that the CSS is slightly different in the JS Bin because of their built-in styles. If 20% / 80% widths don't work, experiment with changing them to e.g. 15% / 80%.

For the character limit, there are two possible approaches.

  • The first is to create a new formula field on your object, e.g. relatedTo.AbbreviatedBody__c, with the formula TRIM(LEFT(Body, 200)). You would then use {!relatedTo.AbbreviatedBody__c} in your template rather than {!relatedTo.Body}. This is probably the best option.

  • The second is to add a size to the .body CSS definition and add the property text-overflow:ellipsis;, which will limit the text to the .body element size. However, this is based on size (e.g. pixels or ems), not characters.

  • I need to use this in email template and not in layouts . I am extremely new to HTML. This v.f template code was written by someone else who has left the company. I need to modify this to get proper indentation of the values brought from the merge fields(Description & Latest Comment) and limit them to only 200 characters. Sorry but I am unable to combine CSS and HTML here which you have provided and I am getting some weird result. – Ruchi Sep 30 '15 at 9:51
  • Yep, the answer is addressing an email template. If you want help applying the example to your code, you're going to need to be more specific than saying you're getting "some weird result". It might be easiest to ask someone in your company who has more experience with HTML / Visualforce. – Rob Sep 30 '15 at 9:56
  • Great, well done Ruchi. I've added a solution for the character limit to the end of my answer. – Rob Sep 30 '15 at 10:26
  • It was working well when I was not providing merge field. But on using them I get some extra things in the email. I have added the screenshot of the original thing which I had posted, and what I got in email. Also I have added the updated code. See the actual description what I had posted in Idea and In email I received things like <div><span style=""><span style="color: #002060;">. – Ruchi Sep 30 '15 at 10:38
  • Double check your use of {!relatedTo.Body}. You need to use an outputText with escape="false" as in the example. – Rob Sep 30 '15 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.