I'm trying to delete gmail history before creating a case comment.
The line of code causing the problem seems to be
.replaceAll('On\\s.+\\sat\\s.+\\s<\\s*\\r*.+@.+>\\swrote:(.|\\n|\\r)*','')
The phrase I am searching for is
On 25 April 2017 at 10:15, Donald Trump < [email protected]> wrote:
The aim being to find this phrase, replace it and any subsequent characters with nothing, effectively deleting it. My regex works with editors on-line as straight java (i.e. with single \ rather than \\ for Apex).
When I test in Sandbox or Production (by sending a mail from the case) it works fine.
When I test in Production (using email to case) the following error is returned
The following errors were encountered while processing an incoming email:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY : EmailMessageCopyToCaseComments: execution of AfterInsert
caused by: System.UnexpectedException: Salesforce System Error: 84857075-252026 (-423225879) (-423225879)
Class.EmailMessageCopyToCaseCommentsTrigger.copyEmailMessagesToCaseComments: line 15, column 1 Trigger.EmailMessageCopyToCaseComments: line 2, column 1
Here's the trigger:
trigger emailMessageCopyToCaseCommentsTrigger on EmailMessage (after insert) {
emailMessageCopyToCaseComments.copyEmailMessagesToCaseComments(Trigger.new);
}
Here's the class:
public class emailMessageCopyToCaseComments
{
public static void copyEmailMessagesToCaseComments(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 +'\n\n';
if (email.TextBody!=null) {
comment.CommentBody = header + email.TextBody.substringbefore ('This message and any attachment are only for the use of the intended recipient')
.substringbefore ('-----Original Message-----')
.substringbefore ('---------- Forwarded message ----------')
.replaceAll('On\\s.+\\sat\\s.+\\s<\\s*\\r*.+@.+>\\swrote:(.|\\n|\\r)*','');
} else if (email.HtmlBody!=null) {
comment.CommentBody = header + email.HtmlBody.substringbefore ('This message and any attachment are only for the use of the intended recipient')
.substringbefore ('-----Original Message-----')
.substringbefore ('---------- Forwarded message ----------')
.replaceAll('\\<.*?>','');
}
comments.add(comment);
}
if (!comments.isEmpty())
{
insert comments;
}
}
}
I believe Marco Casalaina (Werewolf) is author of the original class and trigger which I have copied.