2

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 < donald.trump@america.co.us> 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.

  • Thanks for your suggestion, Christianluul. I haven't tried that with this particular error, but I shall and report back – apexTwin Jul 26 '17 at 10:56
  • Sorry I had not read your question correctly. When you get an error the code must run. – ChristianJuul Jul 26 '17 at 11:00
  • Do you have any validation rules on CaseComment in production that you do not have in the Sandbox? Sometimes when I see an error like this I create a new sandbox and deploy to that, then you should see the same error in the new sandbox. – ChristianJuul Jul 26 '17 at 11:11
  • Thanks again, Christianluul. I will check validation rules. I certainly have a lot of workflow on case comments and incoming mail. – apexTwin Jul 26 '17 at 11:55
  • I have also tested sending a mail from the case in Production and the regex works. The error still generates when email to case is used. The same would be true in Sandbox if I could test with email to case. – apexTwin Jul 26 '17 at 12:00
2

Solution:

I deleted an extraneous ( which I had in the line in production, but not sandbox and added a +, to make the last section

(.+|\\n|\\r)

so the final line which has tested OK is

.replaceAll('On\\s.+\\sat\\s.+\\s<\\s*\\r*.+@.+>\\swrote:(.+|\\n|\\r)*','')
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.