Apex class function requires modification and proper structure. We have the baseline for an Apex class used in Salesforce on an inbound email service. The inbound email service uses this class to parse the email subject and email message body. From the parsing, the class then needs to determine which custom object to write the new record to. The challenge is to clean up this baseline function and make it work. The issue it has at the moment is to determine how many products are ordered and quantities ordered in the inbound email message. Since this email is somewhat dynamic, the class needs to determine if there are more line items in the email body or not... and depending on that result, process it accordingly. The function code is almost there, just needs an experienced programmer..
## Deliverables
Here is a sample of the inbound email and what the program needs to parse:
* Also note that we have control (somewhat) on the email template sent to the apex class:
This is the email:
[store_name]
[oemail]
[ofirstname]
[olastname]
[oaddress]
[ocity]
[ostate]
[ozip]
[ocountry]
[ophone]
<!--START: pass-->[pass]<!--END: pass-->
<!--START: product-->[numitems]
[itemid]
<!--END: product-->Stop
This is a subset of the code. IT is where I think the problem is at the moment. This is coded in Eclipse with the [login to view URL] plugin. We currently have as a baseline that will need the modifications listed in the attachment.
/*
* This is our PDA Inbout Email Service Class
* It serves to capture incoming Shopping Cart emails
* Converts the email to analyze which object
* a new record will be created in.
* Email services are automated processes that use Apex classes
* to process the contents, headers, and attachments of inbound
* email.
*/
global class EmailtoApexInboundOrder implements [login to view URL]
{
public static String getFieldValue(String plainTextBody, String pLabel)
{
Integer startPos = [login to view URL](pLabel);
Integer endPos = [login to view URL]('\n');
return [login to view URL](startPos+[login to view URL](), endPos);
}
global [login to view URL] handleInboundEmail([login to view URL] email,
[login to view URL] envelope)
{
[login to view URL] result = new [login to view URL]();
// Decided not to use this portion for testing.
//String email_subject = [login to view URL](getFieldValue([login to view URL],':'));
//Double mileageInt = [login to view URL](getFieldValue([login to view URL], 'Mileage:'));
//List<Contact> contactResult = new List<Contact>();
//contactName = '%'+contactName+'%';
String order_no = [login to view URL];
String[] emailBody = [login to view URL]('\n', 0);
String ref_selling_site = emailBody[0].substring(0);
String email_address = emailBody[1].substring(0);
String first_name = emailBody[2].substring(0);
String last_name = emailBody[3].substring(0);
String street_address = emailBody[4].substring(0);
String city = emailBody[5].substring(0);
String province = emailBody[6].substring(0);
String zip = emailBody[7].substring(0);
String country = emailBody[8].substring(0);
String phone = emailBody[9].substring(0);
String password = emailBody[10].substring(0);
String quantity1 = emailBody[11].substring(0);
String sku1 = emailBody[12].substring(0);
String quantity2 = null;
String sku2 = null;
String quantity3 = null;
String sku3 = null;
String quantity4 = null;
String sku4 = null;
String quantity5 = null;
String sku5 = null;
String quantity6 = null;
String sku6 = null;
String quantity7 = null;
String sku7 = null;
String quantity8 = null;
String sku8 = null;
String quantity9 = null;
String sku9 = null;
String quantity10 = null;
String sku10 = null;
// The Apex class can consume up to 10 different ordered products for now. This should
// be the maximum amount of skus one order can place for now. This code needs revision if we
// are going to permit more than 10 different products to be purchased and processed by SF.
Integer counterI = 13;
Integer counterII = 14;
// Interger counterIII = 2;
// String counterStringValue = [login to view URL](counterIII);
String stopString = 'Stop';
String nextQuantity = emailBody[counterI].substring(0);
Boolean myresult = [login to view URL](stopString);
while (myresult!=true)
{
if (counterI = 13)
{
quantity2 = emailBody[13].substring(0);
sku2 = emailBody[14].substring(0);
}
else if (counterI = 15)
{
quantity3 = emailBody[15].substring(0);
sku3 = emailBody[16].substring(0);
}
else if (counterI = 17)
{
quantity4 = emailBody[17].substring(0);
sku4 = emailBody[18].substring(0);
}
else if (counterI = 19)
{
quantity5 = emailBody[19].substring(0);
sku5 = emailBody[20].substring(0);
}
else if (counterI = 21)
{
quantity6 = emailBody[21].substring(0);
sku6 = emailBody[22].substring(0);
}
else if (counterI = 23)
{
quantity7 = emailBody[23].substring(0);
sku7 = emailBody[24].substring(0);
}
else if (counterI = 25)
{
quantity8 = emailBody[25].substring(0);
sku8 = emailBody[26].substring(0);
}
else if (counterI = 27)
{
quantity9 = emailBody[27].substring(0);
sku9 = emailBody[28].substring(0);
}
else if (counterI = 29)
{
quantity10 = emailBody[29].substring(0);
sku10 = emailBody[30].substring(0);
}
counterI = counterI + 2;
counterII = counterII + 2;
nextQuantity = emailBody[counterI].substring(0);
myresult = [login to view URL](stopString);
}