Ask Search:
Eric CannadyEric Cannady 

add case comments from email

 We have email to case turned on so that customers can reply to emails and they are added to the case but you then end up with two different areas with communication.  comments and email.  This can be very confusing.  So my question is:  Is it possible to automaticly add the body of emails coming and going as case comments.  This way atleast the comment section has all communicaion about a case....

PS we are in the implication process for using salesforce to track cases but we are really struggling with how to communicate with our customers.  They rely on email and the portal to communicate with us and are not going to be really confused not having one section that contains all communication...
Matt BrownMatt Brown
I can understand why you may feel that this would be confusing to your customers but honestly we have over 500 accounts accessing our Customer Portal and not one has been confused by Emails and Comments as forms of communication. Intuitively our Customers will use Comments after the initial Case Submission and Emails responses to communicate with our Support team and visa versa.

However, if you do want to go down the route of turning emails into case comments here would be a solution:

A workflow rule + an apex trigger would work. 
- Make a custom field on Case called "last email."  Don't show it on any page layout.
- Make a workflow rule on Email Message which copies the text of the email to that field.
- Make an Apex trigger on Case which takes that field and makes a new comment out of it.

Both the instructions and the triggers were pulled from these resources:

trigger commentMove on Case (after update) {
  Case myCase =[0];
  if (myCase.Last_email__c!= null) {
    String caseId= myCase.ID;
    CaseComment cc = new CaseComment(CommentBody=myCase.Last_email__c,parentID=caseId);
    insert cc;

This might also be of help:

Bruno MalnovicBruno Malnovic
It is a bit of a slippery slope. Many of our customers have no problems with the 2 options to communicate. Others do have some issues, mainly due to the complexity of their own internal organizations.

You could always disable Case Comments, but I wouldn't recommend it. A few minutes spent explaining how the portal works and where customers should look for information usually takes care of the problem.

Also consider that when you create a case comment, you have the potential to generate an e-mail to the customer to notify them of the comment. Using a process to copy an e-mail to a comment could trigger a looping scenario where a case comment generates an e-mail, only to generate a case comment and so on.

Eric CannadyEric Cannady
 Thanks for the ideas.  I guess my main concern is what communication channel to use when.  Case comments seem to be what SF wants you to use as the default but you cannot copy other people and the customer cannot reply to the email they must login to the portal.  If we use email they can reply to the email but if they login to the portal they cannot reply to the email there they have to add a case comment. In turn breaking the communication thread also when you view the emails you only see the first line unless you open it making it much harder to quickly view all the communication you have had with a customer...  Not to mention on consultants want to be copied on case communication which then is going to cause a mass amount of workflow rules to cover all the communication channels creating an admin mess...  Sorry for the rant but I thought moving to SalesForce was going to solve a lot of our problems and it will but their integration of communication channels is lacking... 

Thanks again for the ideas and thoughts.

Eric CannadyEric Cannady
 I believe I have found the solution we are looking for in the app exchange  "Email to case Premium"
Tim ForehandTim Forehand
Hey Mattybme1,
I'm not sure if you're still checking this thread, I'm trying the suggestion below, but I keep getting the following error when I attempt to save the trigger:
ErrorError: Compile Error: Incorrect SObject type: Case should be EmailMessage at line 1 column 1

ErrorError: Compile Error: Incorrect SObject type: Case should be EmailMessage at line 1 column 1
Error: Compile Error: Incorrect SObject type: Case should be EmailMessage at line 1 column 1

Any idea why I'm getting this?

Tim F
Tim ForehandTim Forehand
Nevermind, I figured out what I was doing wrong.  Now I'm trying to figure out how to move this into production, since the Enterprise edition doesn't give you an easy way to create NEW triggers.
Matt BrownMatt Brown
@tforehand  have you tried using the Deployments? You create a Change Set and place the Apex Trigger in it and then deploy it back to production. That is if you have done this in your Sandbox environment. If you have used a Dev Edition you will need to use the Eclipse IDE to migrate it to your Production org.
Matt BrownMatt Brown
Also I would take a serious look at the Appexchange App called Email-to-Case Premium as it is so much better than the my original solution and it is not very expensive.
Tim ForehandTim Forehand
Hey Mattybme1,
Thanks for the help on this.  I'm still at it though.  I have managed to get the trigger moved into production, but the way it's written, it's creating case comments from the last email every time any of the case fields gets changed, not just when an email is sent.  Do you have any suggestions?  Unfortunately, I don't have the luxury of paying for the email-to-case premium package.

Matt BrownMatt Brown
So an adjustment to where the Apex Trigger needs to reside. It needs to be on Case Comments if you haven't got it there already. Go Setup>App Setup>Customize>Cases>Case Comments>Triggers.

Also the Last Email field needs to be a long text field if it is not already.
Brent TaylorBrent Taylor
Hi There,

I've installed this in to my sandbox org and it works perfectly, when I try and push it into production it give me the test case errors.

Since I'm not a programmer and this is my first and only piece of code I'll need is there any chance a test class was created for this as well?

Adam WallAdam Wall
@tforehand you don't happen to remember what you were doing wrong, do you?  I'm running into the same error.
Steve PaskiSteve Paski
Setup the above steps to get an Email into Comments and it works great, however the "Created By" is taking on the default case owner from Support Settings.  How do I update the "Created By" with the email address of the email sender?  For some reason I cannot locate the correct "From" field to update,  the obvious fields give me an error of not being defined in the Apex code.
Steve PaskiSteve Paski
Answered my own question, not possible that I can find.
Krishna ShastryKrishna Shastry
@Matt Brown - thanks for providing the sample code. I went to create a new trigger under Case Comments, but I recieve 'reference' errors (I'm assuming because it was initially created for Cases). How would I adapt to create a trigger under case comments? 

meghna mmeghna m
@Matt Brown : We do have a similar requirement .Help Me out

"The case comments of the duplicate cases should get added to the original case which is open".

As per my requirement :

1) Create a case with Subject line  (User ) ----First Case

2) Create another case with Same Subject line [either by (Same user or different user )] .----Second Case

3)Now System should automatically identify it as a " Duplicate case and close the case " (i.e second case)  .

4)Now the Case Comments of the Closed case (i.e second case) should be added to the existing case ( i.e first case) which is open.

For this requirement ,i have written a code which works 90% .But the case comments of the duplicate cases are not getting added to the original case which is open.

Here is the code :

trigger CaseTrigger on Case (after insert) {

    Map<String, Map<Id, Map<Case, Map<Id, CaseComment>>>> subjectCaseCommentsMap = new Map<String, Map<Id, Map<Case, Map<Id, CaseComment>>>>();
    Map<String, Id> subjectOpenCaseId = new Map<String, Id>();
    Map<Id, Case> casesToUpdate = new Map<Id, Case>();
    List<CaseComment> commentsToInsert = new List<CaseComment>();

    // Gather the subjects
    for (Case c : trigger.New) {

        if (!subjectCaseCommentsMap.containsKey(c.Subject)) {

            subjectCaseCommentsMap.put(c.Subject, new Map<Id, Map<Case, Map<Id, CaseComment>>>());

    // Gather all Cases with those subjects & their related CaseComments
    for (Case c : [ SELECT
                        Subject IN :subjectCaseCommentsMap.keySet()
                        IsClosed = FALSE
                    ORDER BY
                        CreatedDate ASC ]) {

        // Add this Case to the map
        if (!subjectCaseCommentsMap.get(c.Subject).keySet().contains(c.Id)) {

            subjectCaseCommentsMap.get(c.Subject).put(c.Id, new Map<Case, Map<Id, CaseComment>>());

        subjectCaseCommentsMap.get(c.Subject).get(c.Id).put(c, new Map<Id, CaseComment>());

        // Take note of the first Case for each subject
        if (subjectOpenCaseId.get(c.Subject) == null) {

            subjectOpenCaseId.put(c.Subject, c.Id);
        system.debug('Line 52: Found a Case ' + c.Subject + '. Related comments: ' + c.CaseComments);
        // Add related CaseComments to the map
        if (!c.CaseComments.isEmpty()) {

            for (CaseComment com : c.CaseComments) {

                subjectCaseCommentsMap.get(c.Subject).get(c.Id).get(c).put(com.Id, com);

    // Now see if any of those Cases need to be closed
    for (String sub : subjectCaseCommentsMap.keySet()) {

        // Only proceed if there are duplicates of this subject
        if (subjectCaseCommentsMap.get(sub).size() > 1) {

            for (Id cId : subjectCaseCommentsMap.get(sub).keySet()) {

                for (Case c : subjectCaseCommentsMap.get(sub).get(cId).keySet()) {

                    // Is this Case the first Case for this subject?
                    if (subjectOpenCaseId.get(sub) == c.Id) {

                        // Do nothing, this is the first Case for this subject

                    } else {

                        // Close this Case, and clone the CaseComments
                        c.Status = 'Closed';

                        casesToUpdate.put(c.Id, c);
                        system.debug('Line 84: Closing Case ' + c.Subject + '. Related comments: ' + c.CaseComments);
                        for (CaseComment com : c.CaseComments) {

                                new CaseComment(
                                    CommentBody = com.CommentBody,
                                    ParentId = subjectOpenCaseId.get(sub)
    system.debug('Line 99: casesToUpdate: ' + casesToUpdate);
    system.debug('Line 100: commentsToInsert: ' + commentsToInsert);
    if (!casesToUpdate.values().isEmpty()) {

        update casesToUpdate.values();

    if (!commentsToInsert.isEmpty()) {

        insert commentsToInsert;

Can any one help me on this .Thanks in Advance .Its very very Urgent.
Sebastiano BertaniSebastiano Bertani
Hello, is there any improvement on this topic? Is the paid app the only real solution or there is someone who will be happy to share a good approach? Thanks!
Ranu AgarwalRanu Agarwal
You can write trigger on 'EmailMessage' object and based on parenId field of this object, you can find cases. And, then create comments for those cases.
To learn about trigger on EmailMessage you can take a look here (
Hope that helps :)