You need to sign in to do that
Don't have an account?
Peter 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;
}
}
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;
}
}
All Answers