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
deplaideplai 

need help for code

I have a trigger that takes incoming/outgoing case emails and makes a copy of the email body into the case comments.  I have written some code to take out any of the reply history by looking for the string "From: " or "wrote: ".  This is to take into account the various email clients and how they might reply back.

 

The idea is that when you reply back to an email it will either say:

From: abc@xyz.com

{email body here}

 

OR

 

On 9/5/12: johndoe wrote:

{email body here}

 

So the code will take anything before the From: or wrote:

 

 

if (em.TextBody.contains('From:')){myPlainText = em.TextBody.substring(0, em.TextBody.indexOf('From:'));}    else if (em.TextBody.contains('wrote:')){myPlainText = em.TextBody.substring(0, em.TextBody.indexOf('wrote:'));}     else {myPlainText= em.TextBody;}

 The problem I'm running into is if different clients have been used in the reply messages with some using "From:" and some using "wrote: ".  My code is looking for "From: " first then stops.  If that "From: " was embedded in a bunch of replies using "wrote: ", then I would have taken a bunch of replies into my comments.

 

Is there a way to write the code so that it will look for whatever is highest on the string and then use that as "myPlainText".

jungleeejungleee

Hi

 

 

Hope the below code helps:

//split the email body line-by-line and store it in a list
list<string> emailBodyByLines = em.textBody.split('\n');
string requiredEmail = null;
	for(string s : emailBodyByLines){
	
		if(requiredEmail==null){
			requiredEmail = s;
		}else{
			requiredEmail=requiredEmail+'\n'+s;
		}
		if(s.contains('From:'){
			myPlainText = requiredEmail;
			break;
		}
		if(s.contains('wrote:')){
			myPlainText = requiredEmail;
			break;
		}
	}
//if the mail doesn't contain either of the keywords, then its a fresh mail and store the mail as is.
	if(myPlainText == null){
		myPlainText = em.textBody;
	}

 

Regards

Sam

maja madi

 

 

 

deplaideplai

Thanks for the suggestion Sam, but I don't think this will work for me quite yet.  What I'm looking for is something that looks for whatever comes first, "From: or wrote" then breaks it from there.