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
ZfactorForceZfactorForce 

Search for Specific Word in Subject Line

Hi,
I have written an email service and I need to find a specific phrase in the subject line and place that phrase into my query for relating case.
The subject can come in such as this "ref ABC company DNA1234567 lets go ref" The specific word i need is DNA1234567... this DNA word can be max 10 characters or less. How do I parse out just DNA1234567 in apex code?

String EmailSubject = email.subject;

Please help
thanks
Z
Best Answer chosen by ZfactorForce
santanu boralsantanu boral
You can use the following code snippet to solve your purpose.
 
String emailSub = 'ref ABC company DNA1234567 lets go ref';
String searchChar = 'DNA';

String returnValue = searchString(emailSub,searchChar);
System.debug(returnValue);

public String searchString(String emailSubject,String searchString)
{
	String[] eSubject = emailSubject.split(' ');
	for( Integer i =0;i<eSubject.size();i++)
	{
		if(eSubject[i].contains(searchString))
		{
			return eSubject[i];
		}
	}
	return null;
}


Please mark the answer as solved. :)

All Answers

santanu boralsantanu boral
You can use the following code snippet to solve your purpose.
 
String emailSub = 'ref ABC company DNA1234567 lets go ref';
String searchChar = 'DNA';

String returnValue = searchString(emailSub,searchChar);
System.debug(returnValue);

public String searchString(String emailSubject,String searchString)
{
	String[] eSubject = emailSubject.split(' ');
	for( Integer i =0;i<eSubject.size();i++)
	{
		if(eSubject[i].contains(searchString))
		{
			return eSubject[i];
		}
	}
	return null;
}


Please mark the answer as solved. :)
This was selected as the best answer
ZfactorForceZfactorForce
Thank you so much it worked like a charm.... Much appreciated.
ZfactorForceZfactorForce
Question, some emails that are coming in has a subject line of 2 spaces between the DNA+number and another word... for exaple:
DNA5360732  Test <== double space between the number 2 and Test...
I just want DNA5360732.... how can i achieve that, i tried changing the code above to split after the value was given and then re-do but it did not work...
Any suggestions?
Thanks
Z
santanu boralsantanu boral
In the line number 14, before returning you can use trim() like 
return eSubject[i].trim();