I am looking to complete the Code Coverage requirement for deploying Apex Classes. I have taken the inbound email class that Salesfoce has made available for use with custom objects, but now I need to test the code to deploy to production. I am struggling to understand the Apex Testing code and have a deadline I am trying to meet.
Any help with constructing the test code for the following Apex Class would be very much appreciated.
global class EmailToSalesForce implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.Inboundenvelope envelope) {
Account account;
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
try {
// Look for account whose name is the subject and create it if necessary
if ([select count() from Account where Name =:email.subject] == 0) {
account = new Account();
account.Name = email.subject;
insert account;
} else {
account = [select Id from Account where Name =:email.subject];
}
// Convert cc'd addresses to contacts
for (String address: email.ccAddresses) {
Contact contact = new Contact();
Matcher matcher = Pattern.compile('<.+>').matcher(address);
// Parse addresses to names and emails
if (matcher.find()) {
String[] nameParts = address.split('[ ]*<.+>')[0].replace('"', '').split('[ ]+');
contact.FirstName = nameParts.size() > 1? nameParts[0]: '';
contact.LastName = nameParts.size() > 1? nameParts[nameParts.size()-1]: nameParts[0];
contact.Email = matcher.group().replaceAll('[<>]', '');
} else {
contact.LastName = address;
contact.Email = address;
}
// Add if new
if ([select count() from Contact where Email =:contact.Email] == 0) {
contact.AccountId = account.Id;
insert contact;
}
}
// Turn email body into note
Note note = new Note();
note.Title = email.fromName + ' (' + DateTime.now() + ')';
note.Body = email.plainTextBody;
note.ParentId = account.Id;
insert note;
// Save attachments, if any
for (Messaging.Inboundemail.TextAttachment tAttachment :email.textAttachments) {
Attachment attachment = new Attachment();
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = account.Id;
insert attachment;
}
for (Messaging.Inboundemail.BinaryAttachment bAttachment :email.binaryAttachments) {
Attachment attachment = new Attachment();
attachment.Name = bAttachment.fileName;
attachment.Body = bAttachment.body;
attachment.ParentId = account.Id;
insert attachment;
}
result.success = true;
result.message = (email.subject + ' matched');
} catch (Exception e) {
result.success = false;
result.message = 'Sorry, an error has occured. Please contact your Salesforce Administrator.';
}
return result;
}
}
PS - i do not know why there is no "code" option for posting in this forum? Maybe i am just using the preformatted text option wrong..?