/**
* Email services are automated processes that use Apex classes
* to process the contents, headers, and attachments of inbound
* email.
create account from email subject
create contacts from email cc
create text and binary attachments if any
*/
global class EmailDemoReceive implements Messaging.Inboundemailhandler {
global Messaging.Inboundemailresult handleInboundemail(Messaging.Inboundemail email, Messaging.Inboundenvelope env ){
Account account;
Messaging.Inboundemailresult result = new Messaging.Inboundemailresult();
try{
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;
}
}
for(Messaging.Inboundemail.Binaryattachment Battachment : email.binaryAttachments){
Attachment a = new attachment();
a.Name= battachment.fileName;
a.body= battachment.body;
a.parentID= account.id;
try{
insert a;}
catch(DMLException e){
system.debug(e.getMessage());
}
}
for(Messaging.Inboundemail.Textattachment tAttachment : email.textAttachments) {
Attachment a = new Attachment();
a.name= tAttachment.fileName;
a.body= blob.valueOf(tAttachment.body);
a.parentID= account.id;
insert a;
}
result.success= true;
}
catch(Exception e){
result.success=false;
result.Message='oops, failed again';
}
return result;
}
}
Unit class for testing this would be
@IsTest
private class EmailDemoReceiveHandlerTests {
// Create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
// Set up your data if you need to
// Create the email body
email.plainTextBody = 'This should become a note';
email.fromAddress ='test@test.com';
String contactEmail = 'jsmith@salesforce.com';
email.ccAddresses = new String[] {'Jon Smith <' + contactEmail + '>'};
email.subject = 'Dummy Account Name 123';
EmailDemoReceive edr = new EmailDemoReceive();
Test.startTest();
Messaging.InboundEmailResult result = edr.handleInboundEmail(email, env);
Test.stopTest();
System.assert (result.success, 'InboundEmailResult returned a failure message');
Account [] accDb = [select ID from Account where name=:email.subject];
System.assertEquals (1, accDb.size(),'Account was not inserted');
Contact [] cDb = [select firstname,lastname from Contact where email=:contactEmail];
System.assertEquals (1, cDb.size(),'Contact was not inserted!');
Contact c = CDb[0];
System.assertEquals ('Jon', c.firstName);
System.assertEquals ('Smith', c.LastName);
Note [] nDb = [select body from Note where ParentID=:accDb[0].id];
System.assertEquals (1,nDb.size(), 'A note should have been attached');
System.assertEquals (email.plainTextBody, nDb[0].body);
}
}
Thanks for sharing this blog post,Nice written skill salesforce Online Training
ReplyDeletePretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing... Salesforce Developer Training
ReplyDelete
ReplyDeleteI found it very explanatory and informative, thank you very much for sharing your knowledge and wisdom with us.
Pinbahis
Hiltonbet
Jojobet
İmajbet
Aresbet
Maltcasino
Marsbahis
Trendbet
SourceEssay experts in GOLD COAST can teach you how to produce a wonderful literature review.
ReplyDeleteA literature review is a thorough examination of the main and secondary sources of data used in a study. Articles, government papers, books, and magazines are just a handful of the tools at your disposal. Each source is described, analysed, and summed up in the literature review. A literature review is necessary for a thesis or dissertation.
Students who are having trouble writing a literature review or are behind on their deadlines should contact SourceEssay professionals right away for the following reasons:
For students in GOLD COAST, SourceEssay writers may give individualised Literature Review Help GOLD COAST.
Because they are created in accordance with university criteria, literature reviews do not need to be expensive. Students can now pay one flat fee regardless of the amount of work they have done. Students in GOLD COAST may now hire experts to help them with their literature reviews; they have a pool of experts who must do numerous quality checks on the literature review to ensure that it is free of plagiarism. The literature review was error-free and adhered to the criteria.
Due to the frequent occurrence of deadlines, students must complete their literary assessments on time. Due to poor time management and a lack of experience, students frequently fail to complete evaluations on time.
The experts at Source Essay make certain that students always achieve their deadlines. Through a skilled customer support team, they communicate with students and give round-the-clock assistance. If students need assistance preparing a literature review, they can contact specialists at any time by chat, phone, or email. These experts double-check assignments before sending them to students for final submission to guarantee that they are error-free and fully unique.