You need to sign in to do that
Don't have an account?
Jessica Mead
Email Service for Email to Lead
Hi -
I am trying to create an Email Service to utilize for an Email to Lead service. I don't want the From/To Address or Contact Name from the typical line, rather I'd like to be able to pull out fields based upon location (these are email templates getting forwarded to me from an email service). I've successfully created the Apex Class and have repeatedly tested it with the provided email, but I'm thorouhgly stuck on creating the test class to move it to production. Any input would be greatly appreciated!
Apex Class:
global class Email2Lead implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope envelope){
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
String[] emailBody = email.plainTextBody.split('\n');
system.debug('------------emailbody------'+emailBody);
String name = emailBody[12].substring(6);
String company = emailBody[13].substring(9);
String location = emailBody[14].substring(10);
String phone = emailBody[15].substring(14);
String Leademail = emailBody[16].substring(7);
String website = emailBody[25].substring(47);
String types = emailBody[26].substring(8);
String currentlyadvertise = emailBody[27].substring(21);
String target = emailBody[28].substring(9);
String zipcode = emailBody[29].substring(11);
Lead[] newLead = new Lead[0];
try {
if(newLead.size()==0)
{
newLead.add(new Lead(
Name__c=name,Company = company,Email = Leademail,
Location__c=Location, LastName=name,
Website = website,Types__c = types,Phone=phone,
Currently_Advertise__c = currentlyadvertise,
Target__c = target,zip_code__c = zipcode));
insert newLead;
}
}
catch (QueryException e) {
}
result.success = true;
return result;
}
}
Test Class (this DOES pass, until I take it to the Validation step):
@isTest
private class TestEmail2Lead{
static testMethod void Email2Lead(){
// create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
// setup the data for the email
email.subject = 'Test Job Applicant';
email.fromname = 'FirstName LastName';
envelope.fromAddress = 'someaddress@email.com';
email.plainTextBody = 'email body\n2225256325\nTitle';
// add an attachment
Messaging.InboundEmail.BinaryAttachment attachment = new
Messaging.InboundEmail.BinaryAttachment();
attachment.body = blob.valueOf('my attachment text');
attachment.fileName = 'textfileone.txt';
attachment.mimeTypeSubType = 'text/plain';
email.binaryAttachments =
new Messaging.inboundEmail.BinaryAttachment[] { attachment };
// add an Text atatchment
Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
attachmenttext.body = 'my attachment text';
attachmenttext.fileName = 'textfiletwo3.txt';
attachmenttext.mimeTypeSubType = 'texttwo/plain';
email.textAttachments = new Messaging.inboundEmail.TextAttachment[] { attachmenttext };
// call the email service class and test it with the data in the testMethod
Email2Lead testInbound=new Email2Lead ();
testInbound.handleInboundEmail(email, envelope);
// query for the lead the email service created
Lead lead = [select id, firstName, lastName, email from lead
where firstName = 'FirstName' and lastName = 'LastName'];
}
}
I am trying to create an Email Service to utilize for an Email to Lead service. I don't want the From/To Address or Contact Name from the typical line, rather I'd like to be able to pull out fields based upon location (these are email templates getting forwarded to me from an email service). I've successfully created the Apex Class and have repeatedly tested it with the provided email, but I'm thorouhgly stuck on creating the test class to move it to production. Any input would be greatly appreciated!
Apex Class:
global class Email2Lead implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope envelope){
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
String[] emailBody = email.plainTextBody.split('\n');
system.debug('------------emailbody------'+emailBody);
String name = emailBody[12].substring(6);
String company = emailBody[13].substring(9);
String location = emailBody[14].substring(10);
String phone = emailBody[15].substring(14);
String Leademail = emailBody[16].substring(7);
String website = emailBody[25].substring(47);
String types = emailBody[26].substring(8);
String currentlyadvertise = emailBody[27].substring(21);
String target = emailBody[28].substring(9);
String zipcode = emailBody[29].substring(11);
Lead[] newLead = new Lead[0];
try {
if(newLead.size()==0)
{
newLead.add(new Lead(
Name__c=name,Company = company,Email = Leademail,
Location__c=Location, LastName=name,
Website = website,Types__c = types,Phone=phone,
Currently_Advertise__c = currentlyadvertise,
Target__c = target,zip_code__c = zipcode));
insert newLead;
}
}
catch (QueryException e) {
}
result.success = true;
return result;
}
}
Test Class (this DOES pass, until I take it to the Validation step):
@isTest
private class TestEmail2Lead{
static testMethod void Email2Lead(){
// create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
// setup the data for the email
email.subject = 'Test Job Applicant';
email.fromname = 'FirstName LastName';
envelope.fromAddress = 'someaddress@email.com';
email.plainTextBody = 'email body\n2225256325\nTitle';
// add an attachment
Messaging.InboundEmail.BinaryAttachment attachment = new
Messaging.InboundEmail.BinaryAttachment();
attachment.body = blob.valueOf('my attachment text');
attachment.fileName = 'textfileone.txt';
attachment.mimeTypeSubType = 'text/plain';
email.binaryAttachments =
new Messaging.inboundEmail.BinaryAttachment[] { attachment };
// add an Text atatchment
Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
attachmenttext.body = 'my attachment text';
attachmenttext.fileName = 'textfiletwo3.txt';
attachmenttext.mimeTypeSubType = 'texttwo/plain';
email.textAttachments = new Messaging.inboundEmail.TextAttachment[] { attachmenttext };
// call the email service class and test it with the data in the testMethod
Email2Lead testInbound=new Email2Lead ();
testInbound.handleInboundEmail(email, envelope);
// query for the lead the email service created
Lead lead = [select id, firstName, lastName, email from lead
where firstName = 'FirstName' and lastName = 'LastName'];
}
}