+ Start a Discussion
Peter CowenPeter Cowen 

move emails to ticket comment

Hi I have created a trigger that should add an email as a ticket comment but it appears not to work. Can you review and let me know where the issue lies? Or if you have a better trigger to do this please let me know

trigger CaseEmails on Case (after insert, before insert) {

List<EmailMessage> emails;

List<CaseComment> comments = new List<CaseComment>();

for (EmailMessage email:emails)

{

Id CaseId = email.ParentId;

CaseComment comment = new CaseComment(ParentId=caseId);

String header = 'From: ' + email.FromName + ' <' + email.FromAddress + '>\n';

header += email.CcAddress!=null?'CC: '+ email.CcAddress + '\n\n':'\n';

if (email.TextBody!=null){

comment.CommentBody = header + email.TextBody; }

else if (email.HtmlBody!=null){

comment.CommentBody = header + email.TextBody; }

comments.add(comment);

}

if (!Comments.isEmpty())

{

insert comments;

}

}
Best Answer chosen by Peter Cowen
Ansh CoderAnsh Coder
You haven't retrived the value of email.
list<emailmessage> emails;
emails=[<SOQL_query>];
//SOQL query statement is missing

 

All Answers

Ansh CoderAnsh Coder
You haven't retrived the value of email.
list<emailmessage> emails;
emails=[<SOQL_query>];
//SOQL query statement is missing

 
This was selected as the best answer
Peter CowenPeter Cowen
Do you know what the SOQL Query would be?