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
Benjamin HimleyBenjamin Himley 

How do I set up an Email Service to automatically generate Leads?

I see that there are multiple apps on the app exchange to do this, however, they depend on the email coming from the lead themselves.  My situation is that I am getting emails from a lead generating company, from the same email everytime, and the lead information is in the content of the email itself.  The emails always come from the same email, and the content is always formatted in the same way. 
From what I understand, I need to set up an email service, then use some kind of Apex code to pull the information from the email, then set up an email forwarding rule in my outlook account so that the emails automatically get sent to salesforce where they are then are turned into leads.  

Is this correct?  If it is, how do I do the Apex coding part?  I have a little bit of coding experience (ruby, javascript), but are there any resources besides the apex code devoloper's guide that someone could point me to?  Going through that whole guide just to write a simple email service seems a bit overkill; has anyone done something similar that I could look at and adjust to my needs?

Thanks so much in advance,

Benjamin

Best Answer chosen by Benjamin Himley
KaranrajKaranraj
Hi Benjamin - You have pass the unique Id of the Record Type not the Name. Include the following line in your code(inside the try block)

Map<String, Schema.SObjectType> sObjectMap = Schema.getGlobalDescribe() ;
Schema.SObjectType s = sObjectMap.get('Lead') ; 
Schema.DescribeSObjectResult resSchema = s.getDescribe() ;
Map<String,Schema.RecordTypeInfo> recordTypeInfo = resSchema.getRecordTypeInfosByName(); 
Id rtId = recordTypeInfo.get('Housing Counseling').getRecordTypeId();


newLead.add(new Lead(RecordTypeId = rtId,




All Answers

KaranrajKaranraj
Hi Benjamin,

Try this below sample code to get start too generate lead from email. Make sure to include the required fields while creating lead in this email service class

global class CreateTaskEmailExample implements Messaging.InboundEmailHandler {
 
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                       Messaging.InboundEnvelope env){
 
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    
    
    String[] emailBody = email.plainTextBody.split('\n', 0);
    String firstName = emailBody[0].substring(5);
    String phoneNumber = emailBody[0].substring(6);
    String city = emailBody[1].substring(5);
    Lead[] newLead = new Lead[0];
     try {
       newLead.add(new Lead(Name =  firstName,Phone = phoneNumber,City = city));
       insert newLead;        
    }
    
   catch (QueryException e) {
       
   }
   
   result.success = true;
   return result;
  }
}


Assuming the email template in the below formate
Name : Karanraj
Phone: 999999999
City: Chennai


Here is the link for retrieving information from the email message in the apex class http://developer.force.com/cookbook/recipe/retrieving-information-from-incoming-email-messages

Thanks,
Karan




Benjamin HimleyBenjamin Himley
Thanks so much Karan!  That got me going to a good start, but I am getting this error when trying to save the apex class:

Error: Compile Error: Invalid initial expression type for field RecordType, expecting: SOBJECT:RecordType (or single row query result of that type) at line 44 column 42

Here is my code:

global class CreateTaskEmailExample implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,

                                                       Messaging.InboundEnvelope env){


    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
     

    String[] emailBody = email.plainTextBody.split('\n', 0);

    String firstName = emailBody[1].substring(10);
    
    String lastName = emailBody[2].substring(10);

    String address = emailBody[3].substring(8);

    String state = emailBody[4].substring(5);

    String zipcode = emailBody[5].substring(7);

    String city = emailBody[6].substring(4);

    String leadEmail = emailBody[7].substring(12);

    String phone = emailBody[8].substring(8);

    String paymentsBehind = emailBody[11].substring(15);

    String loanAmount = emailBody[13].substring(11);

    String estimatedHomeValue = emailBody[14].substring(21);

    String creditLevel = emailBody[17].substring(12);

    String interestRate = emailBody[20].substring(14);


    Lead[] newLead = new Lead[0];

     try {

       newLead.add(new Lead(RecordType = 'Housing Counseling',
       FirstName=firstName,LastName=lastName,Phone = phone,Email = leadEmail,
       Street = address,State = state,City = city,PostalCode = zipcode,
       Payments_Behind__c = paymentsBehind,Total_Amount__c = loanAmount,
       Estimated_Home_Value__c = estimatedHomeValue,
       Credit_Level__c = creditLevel,Interest_Rate__c = interestRate));

       insert newLead;       
    }
     
   catch (QueryException e) {
        
   }
    

   result.success = true;

   return result;

  }

}


KaranrajKaranraj
Hi Benjamin - You have pass the unique Id of the Record Type not the Name. Include the following line in your code(inside the try block)

Map<String, Schema.SObjectType> sObjectMap = Schema.getGlobalDescribe() ;
Schema.SObjectType s = sObjectMap.get('Lead') ; 
Schema.DescribeSObjectResult resSchema = s.getDescribe() ;
Map<String,Schema.RecordTypeInfo> recordTypeInfo = resSchema.getRecordTypeInfosByName(); 
Id rtId = recordTypeInfo.get('Housing Counseling').getRecordTypeId();


newLead.add(new Lead(RecordTypeId = rtId,




This was selected as the best answer