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
Krish NKrish N 

Parse email body using strings to create case and populate it's fields

Hello Everyone,
Need a help with email services. I'm trying to create cases throgh 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 infromation should be popualted in case descriton 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.
User-added image
Mario PariseMario Parise
You'll need to use the Pattern Class (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_pattern_methods.htm#!) to perform some Regex searches on your text. Unfortunately it's one of the less documented features.

What I would do is use an online regex editor (such as regexr (https://regexr.com/)) until you've got a Regular Expression ("regex") that matches the text you're looking for. For example, you might look for text that begins with "Problem Description" and ends when it finds "Assignment", assuming the emails always follow that format.

Once you have your regular expression, you can use the Pattern Class to do this automatically to identify your key text.

Repeat this process for any specific pieces of text you need to identify.
Krish NKrish N
Hello Mario, Could you please provide me with an example. Woudn't splitting the email body work?  
Newbie__SalesforceNewbie__Salesforce
Hey Krish


You can use the following for your requirements

substringBetween(open, close) - Returns the substring that occurs between the two specified Strings.


 
Krish NKrish N
Hello Newbie_Salesforce, Thank you! Can you please provide me the logic with an example w.r.t to my requirement.
Mario PariseMario Parise
I have to support Newbie_Salesforce's solution... I didn't know about that method. Very cool! And much easier than my regex approach.

It would work a little like this:
String emailBody = 'WHATEVER YOUR EMAIL BODY OBJECT IS';

String problemDescription = emailBody.substringBetween('Problem Descriotion: ', 'Assignment');

String productWarranty = emailBody.substringBetween('Warranty: ', '');

Case myCase = new Case(); // Or however you went about generating your case object.

myCase.description = problemDescription;
myCase.product_warranty__c = productWarranty;

// Do anything else you'd like with myCase. Then:

upsert myCase; // upsert will create the case if it doesn't exist yet, or update it if it does.
Documentation link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#!)
Krish NKrish N
Thank you so much Mario. That was really helpful. I will test using this logic and update.
NyshaaNyshaa
Could you please provide me the whole code
I want to implement the same for my Lead object.
I want to map the Title,First Name, Main Phone no., Mobile Phone no., Subject and Query Detail.
Andrew Park 21Andrew Park 21
Hi Krish, 

Were you able to come up with a solution to this request?