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
Ajay_SFAjay_SF 

how to parse the following text email body?

Hi! What's the best way to parse the following text email body? Email body-
This is a detailed summary of your live recurring billing transactions for the 24 hours prior to the time of this e-mail.


Successful Recurring Billing Transactions:

Credit Card transactions
The following profiles have successfully charged customer credit cards:

Profile ID: 12
Profile Name: DONOR_1
Payment Number: 5
Retry Number: 0
PNRef: BL001YY
Transaction Time: 29-Apr-21  06:26 AM
Amount: 50.00

Profile ID: 13
Profile Name: DONOR_2
Payment Number: 5
Retry Number: 0
PNRef: BL001XX
Transaction Time: 29-Apr-21  06:26 AM
Amount: 50.00


Total Number of Approved Transactions: 2
Total Approved Amount: 100.00


Declined Recurring Billing Transactions

Credit Card transactions
The following profiles have failed to charge customer credit cards for the current payment period:

Total Number of Declined Transactions: 0
Total Declined Amount: 0.00



We thank you for your business.

Sincerely,

PayPal Merchant Services

I'm looking to build a List<TransactionInformation>, something like this-​​
public class TransactionInformation {
    public String ProfileID;
    public String ProfileName;
    public Integer PaymentNumber;
    public String RetryNumber;
    public String PNRef;
    public String TransactionTime;
    public Decimal Amount;
}

 
ShivankurShivankur (Salesforce Developers) 
Hi Ajay,

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

Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_using.htm

RegExr.com: Very useful tool for writing proper Regular Expression patterns. Here's one that will grab the "PaymentNumber" from your email body:
/Payment Number:.*/g
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 (https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm).

Example:
String keyValue = 'Profile ID: 12';
String key = 'Profile ID: ';
Integer keyLength = key.length();
String value = keyValue.right(keyLength);  // value equals '12'
Then just use the obtained string values as per the requirement and set the relevant fields equal to the values you've derived!

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.