function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Derhyk Doggett -Derhyk Doggett - 

Incoming Email Case Comments - Hyphens Causing Break

Hi All,
We have a package deployed similar to the links below in our org to allow incoming case emails to be appended to the case comments
https://success.salesforce.com/answers?id=90630000000glk9AAA
https://developer.salesforce.com/forums/?id=906F000000091R2IAI

The problem on some comments is they break when multiple Hyphens in a row occur.
I believe this is because this is how the code determines the break between a reply so the entire email thread is not included in the comment, only the most recent reply is included.

Does anyone have any ideas for a workaround?
Thank you!

Examples and code below.
This comment from email:
SDE> show service spb connections 

Id Type Failures OperStatus AdminStatus ConfiguredURI ConnectedURI LastTimeConnected 
----- ------------------- -------- ----------- ----------- ----------------------------- -------------- ----------------------- 

Id APIVersion 
----- ---------- 
11863 [2] 

SDE> 

SDE> show service spb connections diagnostics 

Diagnostic Result 
--------------- ------- 
ResolveHostname n/a 
PingIps Success 
ClientProcess Success 
ServerSocket Success 
BrokerMessage Success

Becomes:
SDE> show service spb connections

Id Type Failures OperStatus AdminStatus ConfiguredURI ConnectedURI
LastTimeConnected

Here is the Class handling the Incoming Emails and Appending the Case Comment:
public class EmailMessageCopytoCaseCommentHelper {
    public static void copyEmailMessagesToCaseComments(List<EmailMessage> emails){
		integer MAXSIZE = 3900;
        List<CaseComment> comments = new List<CaseComment>();
        for (EmailMessage email:emails){
            Id caseId = email.ParentId;
            CaseComment comment = new CaseComment(ParentId=caseId);
            comment.IsPublished=true;
            String header = 'From: '+ email.FromName + ' <' + email.FromAddress + '>\n';
            header += 'To: '+ email.ToAddress + '\n';
            header += email.CcAddress!=null?'CC: '+ email.CcAddress + '\n\n':'\n';
            Integer worknoteStart=0;
            Integer headerSize = header.length();
            String cbody='';
			if (email.TextBody!=null) {
   				String body = email.TextBody;
				Integer results = body.length();
				Integer worknoteEnd = body.IndexOf('-----');
				if(worknoteEnd+headerSize>MAXSIZE){
					worknoteEnd = MAXSIZE - headerSize;
				}else if(worknoteEnd==-1){
					worknoteEnd = (results+headerSize > MAXSIZE)? (MAXSIZE - headerSize):results;
				}
    			cbody = header + body.substring(worknoteStart, worknoteEnd); 
    			system.debug('\n\nTEXTBODY'+cbody);
			} else if (email.HtmlBody!=null) {
				String body = email.HtmlBody;
				Integer results = body.length();
				Integer worknoteEnd = body.IndexOf('-----');
				if(worknoteEnd+headerSize>MAXSIZE){
					worknoteEnd= MAXSIZE - headerSize; 
				}else if(worknoteEnd==-1){
					worknoteEnd = (results+headerSize > MAXSIZE) ? (MAXSIZE - headerSize):results;
				}
				cbody = header + body.substring(worknoteStart, worknoteEnd).replaceAll('\\<.*?>','');
				system.debug('\n\nHTMLBODY'+cbody);
			}
			
			if(cbody.length()>(MAXSIZE-1)){
				comment.CommentBody=cbody.left((MAXSIZE-1));
                Integer chkbodysize=comment.CommentBody.length();
                system.debug(chkbodysize);
			}else{
				comment.CommentBody=cbody;
			} 
			comments.add(comment);
		}
        try{
        	if (!comments.isEmpty()){
            	insert comments;
        	}	
        }catch(DMLException d){
        	throw new SVException('Error Copying Email to Comment: Field(s): '
        					+d.getDMlFieldNames(0)
        					+' Status: '
        					+d.getDmlType(0)
        					+ ' Body Size:'
        					+comments[0].CommentBody.length()
        					+' ID REF:'
        					+comments[0].ParentId);
        					
        }
        
    }
}

Thanks

-derhyk

 
JeffreyStevensJeffreyStevens
Ya - it's definitly the dashes (-)  that is causing the problem.  Looks like the code is thinking that when it see's 5 dash's in a row - that is the end of the message.  

I THINK that at line#24 and #35 - I would just make cBody to the end of the message  (body.length) - but I'm not 100% sure on that.