Need a help with email services. I'm trying to create cases through emails by using InboundEmailHandler. Our customers have a predefined email format (please check the screenshot). Once the email is sent to our organization, a case has to be created but it shouldn't just dump the email body in the description field. Each of the value in the email belongs to a particular field of case object. I've researched online and found out that String.Split is the way to go. However, I'm having trouble to write the code (still an amateur at Apex). Can any one please help me how to split the lines of email body and assign them to fields in the case object. Please explain it through w.r.t to the email body provided. Thank you!

For ex: Problem Description information should be populated in case description field

Warranty information should be populated in product_warranty__c custom field.

P.S: We only get emails in text format. No html is involved.

enter image description here


Sure. Here's what you'll need to work your way through it:

Regular Expressions in Apex. Using these will make it easier to get the "key" ("Region", for example) and the value between the end of the key and the following line break.


RegExr.com: Very useful tool for writing proper Regular Expression patterns. Here's one that will grab the "Division" from your email body:


From there, you could use the String.right() method to remove the key, and you'll be left with just the value. All the Apex String methods can be found here.


String keyValue = 'Region: Northwest';
String key = 'Region: ';
Integer keyLength = key.length();
String value = keyValue.right(keyLength);  // value equals 'Northwest'

Then just create your case and set the relevant fields equal to the values you've derived!

There are plenty of other (likely better) ways to go about this. Since you're still new to Apex, go ahead and give it a try on your own first. If you get stuck, post your code here and I'm certain the community will help you out!

  • Hey Shashank -- were you able to get this working? – Shane Steinfeld Sep 30 '18 at 21:30

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.