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
amritamrit 

Emai to Lead

Hi,

  I want to create lead from email .I tried one appexchange but its not creating lead.Can anyone tell me a solution for this

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
kritinkritin

You can achieve your requirement with developing one Apex class and one Email Service. here this Email Service will consume your Apex Class.

 

Create below Class :

 

Global class unsubscribe implements Messaging.inboundEmailHandler{

Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope env )
 {

// Create an inboundEmailResult object for returning
//the result of the Apex Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
 
 String strEmailId = email.fromAddress.toLowerCase();
 String strSubject = email.subject.toLowerCase();

 

 //Create a new test Lead and insert it in the Test Method 
 integer iCount;
 iCount = [select count() from Lead where Email=:email.fromAddress];
 
   if (iCount==0)
   {   
   Lead l = new lead(
            lastName=strEmailId,
        Company='MARCOM_EMAIL',
            Email=strEmailId,
            HasOptedOutOfEmail=false,
            OwnerId='00540000000z3vo', //Change this id with user's id to whome you want to assign this lead.
            Number_of_Attachment__c=iCount);
   insert l;
   }
 
result.success = true;
return result;
    }  
   
    // Test method to ensure you have enough code coverage
    // Have created two methods, one that does the testing
    // with a valid "unsubcribe" in the subject line
    // and one the does not contain "unsubscribe" in the
    // subject line
   
static testMethod void testUnsubscribe() {

// Create a new email and envelope object
   Messaging.InboundEmail email = new Messaging.InboundEmail() ;
   Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();

// Create a new test Lead and insert it in the Test Method       
   Lead l = new lead(firstName='Rasmus',
            lastName='Mencke',
            Company='Salesforce',
            Email='rmencke@salesforce.com',
            HasOptedOutOfEmail=false
            );
   insert l;

// Create a new test Contact and insert it in the Test Method 
   Contact c = new Contact(firstName='Rasmus',
                lastName='Mencke',
                Email='rmencke@salesforce.com',
                HasOptedOutOfEmail=false);
   insert c;
  
   // test with subject that matches the unsubscribe statement
   email.subject = 'test unsubscribe test';
   env.fromAddress = 'rmencke@salesforce.com';
  
   // call the class and test it with the data in the testMethod
   unsubscribe unsubscribeObj = new unsubscribe();
   unsubscribeObj.handleInboundEmail(email, env );
                       
   }
 
static testMethod void testUnsubscribe2() {

// Create a new email and envelope object
   Messaging.InboundEmail email = new Messaging.InboundEmail();
   Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

// Create a new test Lead and insert it in the Test Method       
   Lead l = new lead(firstName='Rasmus',
            lastName='Mencke',
            Company='Salesforce',
            Email='rmencke@salesforce.com',
            HasOptedOutOfEmail=false);
   insert l;

// Create a new test Contact and insert it in the Test Method   
   Contact c = new Contact(firstName='Rasmus',
                lastName='Mencke',
                Email='rmencke@salesforce.com',
                HasOptedOutOfEmail=false);
   insert c;
  
   // Test with a subject that does Not contain unsubscribe
   email.subject = 'test';
   env.fromAddress = 'rmencke@salesforce.com';

   // call the class and test it with the data in the testMethod
   unsubscribe unsubscribeObj = new unsubscribe();
   unsubscribeObj.handleInboundEmail(email, env );                     
   }   
  
}

 

Then after goto:

Setup->App Setup-> Develop-> Email Service

click on new, provide Name =Test

Apex Class=unsubscribe & Check active checkbox=true

then click on Save and New Email Address:

now provide values:

Address=test

Active=true

Context User: select your name

Accept Email from: gmail.com

                               hotmail.com

then click on save

And now see the related listEmail Address:

here you will fine one email Address like as test@f-12gros6qqXXXXXXXX.in.salesforce.com

for testing send one mail to this Address test@f-12gros6qqXXXXXXXX.in.salesforce.com and see that the lead goes created in salesforce Org or not.

 

Note:- Please make sure that do not distribute this your created salesforce email address to anyone & save from missuse of it.

 

Thanks :smileyvery-happy:

kritin

 

All Answers

kritinkritin

You can achieve your requirement with developing one Apex class and one Email Service. here this Email Service will consume your Apex Class.

 

Create below Class :

 

Global class unsubscribe implements Messaging.inboundEmailHandler{

Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope env )
 {

// Create an inboundEmailResult object for returning
//the result of the Apex Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
 
 String strEmailId = email.fromAddress.toLowerCase();
 String strSubject = email.subject.toLowerCase();

 

 //Create a new test Lead and insert it in the Test Method 
 integer iCount;
 iCount = [select count() from Lead where Email=:email.fromAddress];
 
   if (iCount==0)
   {   
   Lead l = new lead(
            lastName=strEmailId,
        Company='MARCOM_EMAIL',
            Email=strEmailId,
            HasOptedOutOfEmail=false,
            OwnerId='00540000000z3vo', //Change this id with user's id to whome you want to assign this lead.
            Number_of_Attachment__c=iCount);
   insert l;
   }
 
result.success = true;
return result;
    }  
   
    // Test method to ensure you have enough code coverage
    // Have created two methods, one that does the testing
    // with a valid "unsubcribe" in the subject line
    // and one the does not contain "unsubscribe" in the
    // subject line
   
static testMethod void testUnsubscribe() {

// Create a new email and envelope object
   Messaging.InboundEmail email = new Messaging.InboundEmail() ;
   Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();

// Create a new test Lead and insert it in the Test Method       
   Lead l = new lead(firstName='Rasmus',
            lastName='Mencke',
            Company='Salesforce',
            Email='rmencke@salesforce.com',
            HasOptedOutOfEmail=false
            );
   insert l;

// Create a new test Contact and insert it in the Test Method 
   Contact c = new Contact(firstName='Rasmus',
                lastName='Mencke',
                Email='rmencke@salesforce.com',
                HasOptedOutOfEmail=false);
   insert c;
  
   // test with subject that matches the unsubscribe statement
   email.subject = 'test unsubscribe test';
   env.fromAddress = 'rmencke@salesforce.com';
  
   // call the class and test it with the data in the testMethod
   unsubscribe unsubscribeObj = new unsubscribe();
   unsubscribeObj.handleInboundEmail(email, env );
                       
   }
 
static testMethod void testUnsubscribe2() {

// Create a new email and envelope object
   Messaging.InboundEmail email = new Messaging.InboundEmail();
   Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

// Create a new test Lead and insert it in the Test Method       
   Lead l = new lead(firstName='Rasmus',
            lastName='Mencke',
            Company='Salesforce',
            Email='rmencke@salesforce.com',
            HasOptedOutOfEmail=false);
   insert l;

// Create a new test Contact and insert it in the Test Method   
   Contact c = new Contact(firstName='Rasmus',
                lastName='Mencke',
                Email='rmencke@salesforce.com',
                HasOptedOutOfEmail=false);
   insert c;
  
   // Test with a subject that does Not contain unsubscribe
   email.subject = 'test';
   env.fromAddress = 'rmencke@salesforce.com';

   // call the class and test it with the data in the testMethod
   unsubscribe unsubscribeObj = new unsubscribe();
   unsubscribeObj.handleInboundEmail(email, env );                     
   }   
  
}

 

Then after goto:

Setup->App Setup-> Develop-> Email Service

click on new, provide Name =Test

Apex Class=unsubscribe & Check active checkbox=true

then click on Save and New Email Address:

now provide values:

Address=test

Active=true

Context User: select your name

Accept Email from: gmail.com

                               hotmail.com

then click on save

And now see the related listEmail Address:

here you will fine one email Address like as test@f-12gros6qqXXXXXXXX.in.salesforce.com

for testing send one mail to this Address test@f-12gros6qqXXXXXXXX.in.salesforce.com and see that the lead goes created in salesforce Org or not.

 

Note:- Please make sure that do not distribute this your created salesforce email address to anyone & save from missuse of it.

 

Thanks :smileyvery-happy:

kritin

 

This was selected as the best answer
amritamrit

Thanks for your reply .Is that only possible for gmail and hotmail.com

kritinkritin

you can add more domain as you wish.

like yahho.com, yahoo.co.in

@live.in etc....

 

 

Regards:

kritin

amritamrit

thanks for your reply. In lead I want to display name in the field 'Name' instead of mail address .I tried by changing fromAddress to fromName.But its not getting.Any idea about that

amritamrit

Hi,

 

I want to attach a file and im using Messaging.InboundEmail.TextAttachment but im getting null pointer exception..And the code is not working..

 

Global class unsubscribe implements Messaging.inboundEmailHandler{

    Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope env )
    {

    // Create an inboundEmailResult object for returning
    //the result of the Apex Email Service
    Lead lead;
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    
 
    String strEmailId = email.fromAddress;
    String strSubject = email.subject;
    String myText=email.plainTextBody;
    String myFromName = email.fromName;
    
    for (integer i = 0 ; i < email.headers.size() ; i++) {
        if (email.headers[i].name.equals('From')) {
            String fromAddress = email.headers[i].value;
            myFromName = fromAddress.split('<')[0];
        }
    }
    
    //Create a new test Lead and insert it in the Test Method
    integer iCount;
    iCount = [select count() from Lead where Email=:email.fromAddress];
 
    if (iCount==0)
    {   
        Lead l = new lead(
        lastName= myFromName, //strEmailId,
        Company=myFromName,
        Email=strEmailId,
        LeadSource='OnlineEmailEnquiry',
        Description=strSubject+'\n'+myText,
        OwnerId='00590000000OJ0w'); //Change this id with user's id to whome you want to assign this lead.
        insert l;    
    }
    System.debug('Content of email: ' +email);
    for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
      Attachment attachment = new Attachment();
      if(tAttachment != null)
     
      attachment.Name = tAttachment.fileName;
      attachment.Body = Blob.valueOf(tAttachment.body);
      attachment.ParentId = lead.Id;
      insert attachment;
    }
    result.success = true;
    return result;
    } 

 

amritamrit

Hi,

 

Thanks to all.Now Email to Lead is working fine.But i want to know How we can configure email to lead using outlook express.I want to forward a person's mail eg:X  to salesforce from my account say Y  using salesforce outlook so that it will create lead in salesforce with X name and email id.But when i configured its creating alead with  X's name but showing Y's email id.Please help me to resolve this

 

 

Thanks

vibrationvibration

Company address is not supported in email address. for example IBM, infosys etc . but yahoo, hotmail ,gmail. all are working nice.

Harish1234Harish1234
Amrit 
Where as i am trying to create email to lead it's not creating

Global class unsubscribe implements Messaging.inboundEmailHandler{
Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope env )
 {
// Create an inboundEmailResult object for returning
//the result of the Apex Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
 
 String strEmailId = email.fromAddress.toLowerCase();
 String strSubject = email.subject.toLowerCase();
 
 //Create a new test Lead and insert it in the Test Method 
 integer iCount;
 iCount = [select count() from Lead where Email=:email.fromAddress];
 
   if (iCount==0)
   {   
   Lead l = new lead(
            lastName=strEmailId,
        Company='MARCOM_EMAIL',
            Email=strEmailId,
            HasOptedOutOfEmail=false,
            OwnerId='005O00000027lwF',//Change this id with user's id to whome you want to assign this lead.
            Number_of_Attachment__c=iCount);
   insert l;
   }
 
result.success = true;
return result;
    }