Including Case Comments in Case Assignment Email

Including case comments in the case assignment email allows the recipient to see exactly what is going on before even logging in to Salesforce.

You need a visualforce email template for this. You can use an apex:repeat component in your email template, but there is no sort order on those. Really, as best as I can find, its just random.

So I went ahead and created a custom component to select comments and sort. Now i can include just the last 3 comments to keep it simple, and its sorted by date, with most recent first.

Credit: This StackExchange post was hugely helpful in making this happen

I altered the solution a bit to use an apex:repeat in a table in the custom component, instead of a datalist, as i found the formatting better. The code for controller, component, test class, and vf email template is all copied below for your pleasure.

controller

public class SortedCaseCommentsController {
  public Id     AttributeCaseId    {get; set;}

  public List<CaseComment> getCaseComments() {
    return [
      SELECT CommentBody, CreatedById, CreatedBy.Name, CreatedDate
      FROM CaseComment
      WHERE ParentId = :this.AttributeCaseId
      ORDER BY CreatedDate DESC
    ];
  }
}

component:


<apex:component controller="SortedCaseCommentsController" access="global">
    <apex:attribute name="caseId" type="Id" assignTo="{!AttributeCaseId}" description="Case Id" />
    <table>
        <apex:repeat value="{!CaseComments}" var="caseComment">
            <tr>
                <th>Created Date</th>
                <th>Created By</th>
                <th>Comment</th>
            </tr>
            <tr>
                <td>
                    <apex:outputText value="{0, date, MMMM d','  yyyy}">
                        <apex:param value="{!caseComment.CreatedDate}" /> </apex:outputText>.
                </td>
                <td>
                    {!caseComment.CreatedBy.Name}
                </td>
                <td>{!caseComment.CommentBody}</td>
            </tr>
        </apex:repeat>
    </table>
</apex:component>

test class:

@istest

private class SortedCaseCommentsTest {

    static testmethod void validateSortedCaseComments() {
        case c = new
        case (Subject = 'Test Case', Status = 'New');
        insert c;

        caseComment cc = new CaseComment(CommentBody = 'abc', parentid = c.id);
        insert cc;

        SortedCaseCommentsController sc = new SortedCaseCommentsController();
        sc.AttributeCaseId = c.Id;
        sc.getCaseComments();
    }

}

visualforce email template
note that using apex outputfield on the description field is necessary for it to display properly. Using relatedTo.Description smushes it together into a single paragraph, no line breaks.

<messaging:emailTemplate subject="Case # {!relatedTo.CaseNumber}: {!relatedTo.Subject} has been assigned to {!relatedTo.Owner.Name}
 " recipientType="User" relatedToType="Case">
    <messaging:htmlEmailBody>
        <STYLE type="text/css">
            TH {
                font-size: 11px;
                font-face: arial;
                background: #CCCCCC;
                border-width: 1;
                text-align: center
            }

            TD {
                font-size: 11px;
                font-face: verdana
            }

            TABLE {
                border: solid #CCCCCC;
                border-width: 1;
                padding: 1 2 1 2;
            }

            TR {
                border: solid #CCCCCC;
                border-width: 1
            }

            CAPTION {
                font-size: 14px;
                font-face: verdana, text-decoration: underline
            }

            P {
                line-height: 150%;
                font face: arial;
                font-size: 12px;
            }
        </STYLE>

        <p>
            *** NEW CASE ASSIGNMENT NOTIFICATION ***
        </p>

        <p>
            <b><u>Case Details:</u></b>
            <br /> Assigned to {!relatedTo.Owner.Name}
            <br /> Created By: {!relatedTo.CreatedBy.Name}
            <br /> Case #: {!relatedTo.CaseNumber}
            <br /> Case status: {!relatedTo.Status}
            <br /> Case Priority: {!relatedTo.Priority}
            <br /> Subject: {!relatedTo.Subject}
            <br />
        </p>

        <p>
            <a href="{!LEFT($Api.Partner_Server_URL_140, FIND(" .com/ ",$Api.Partner_Server_URL_140)+3)}/{!relatedTo.Id}">
Click on the link to access the case. </a>
        </p>

        <p>
            <b><u>Description:</u></b>
            <br />
            <apex:outputField value="{!relatedTo.Description}"></apex:outputField>
        </p>

        <p><b><u>Case Comments</u></b>
            <br />
            <c:sortedCaseComments caseId="{!relatedTo.Id}" />
        </p>

    </messaging:htmlEmailBody>
</messaging:emailTemplate>```

 
9
Kudos
 
9
Kudos

Now read this

Takeaways from a short Dreamforce

I came, I saw U2, I’m flying home. My dreamforce is over, though today is a huge day with all of the product keynotes. Here are my initial takeaways this year, from the perspective of a declarative developer at 30,000 feet. Lightning is... Continue →