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
tommytxtommytx 

Email to Lead (Email2Lead) not working out for me.

Does anyone have suggestions on where I can find detailed examples of using email incoming to insert leads into sales force... I have used the web2lead and its ok... but my leads are coming by email so I need to go Email2lead not web2lead.

I can find lots of stuff that I can understand regarding Email2Contacts or Email2Accounts, but little to nothing on Email2lead processes...

 

Below is a sample of the many examples of Email2Account or Contact.... and that is something similar to what I am looking for but needs to be for leads and not contacts...

 

Code:

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;
}
}

End Code:

 

IN a nutshell here is what I need to do:

*****************************************

Parse through the incoming email message and look for the required information. For example:

An email comes in with the following:

 

Email body begin:
*****************

1. A new user has registered at http://www.mydomain.com/idx/register.html.

2. The following information was collected about the user:

3. Viewed: 6296351, 5055852, 7545845
4. First Name: Elvis
5. Last Name: Presley
6. Phone: 281-363-7159
7. Email: elvis@presley.com
8. Opt Searches: in

Email body End:
****************


// Retrieves content from the body of the email.  
// Splits each line by the terminating newline character 
// and looks for the position of the phone number and city 
   
String[] emailBody = email.plainTextBody.split('\n', 0);
String First_Name = emailBody[4].substring(12);
String Last_Name = emailBody[5].substring(12);
String Lead_Phone = emailBody[6].substring(7);
String Lead_Email = emailBody[7].substring(8);


The stringy thing above works great to parse the data but I cannot figure out how to insert it into the lead.

So I want to do something like
lead = new Lead;

lead.FirstName = First_Name;

lead.LastName = Last_Name;

lead.Phone= Lead_Phone;

lead.Email = Lead_Email;

insert lead;

 

So bottom line is that I need to understand how to add the new lead information from the email to the Sales Force Lead data base and not the Contact Data Base..

 

Any push in the right direction will be greatly appreciated..

 

Thanks