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
NasirNasir 

Email To Lead (Urgent)

Hi

 

I had written a code for email to lead which creates a lead in salesforce.This is working currently and the email address comes who is sending the mail.but the requirement got changed.

 

Suppose a "student" (jawed@gmail.com)send a mail to Nasir whose email id is(nasir@gmail.com) ,and Nasir forward this email to email service in salesforce.A lead should create in salesforce with email of the student(jawed@gmail.com).Currently it is creating a lead with email of Nasir(nasir@gmail.com).

 

Below is my code

Global class unsubscribe implements Messaging.inboundEmailHandler{
    Lead lead;
    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;
    String strSubject = email.subject;
    String myText=email.plainTextBody;
    String myFromName = email.fromName;
   // TextBody = email.plainTextBody,
     //String HtmlBody = email.htmlBody;
   
    //Create a new test Lead and insert it in the Test Method 
    integer iCount;
    iCount = [select count() from Lead where Email=:email.fromAddress];
    system.debug('icount'+icount);
    if (iCount==0)
    {   
        lead = new Lead(lastName=myFromName,Company=myFromName,Email=strEmailId,LeadSource='OnlineEnquiry',Description=strSubject+'\n'+myText,OwnerId='00590000000OJ0w'); //Change this id with user's id to whome you want to assign this lead.
        insert lead;    
       
    }
    if (email.binaryAttachments!=null && email.binaryAttachments.size() > 0) {
        for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
            
                    Attachment newAttachment = new Attachment();
                      newAttachment.ParentId =lead.id;
                    //system.debug('AttachmentIDDDDDDDDDDDDd'+newAttachment.ParentId);
                     newAttachment.Name = email.binaryAttachments[i].filename;
                     newAttachment.Body = email.binaryAttachments[i].body;
                    insert newAttachment;
                }
    }
    
    
    result.success = true;
    return result;
    
    }  


    /* Only for test case */

    // 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();
        Messaging.InboundEmail.BinaryAttachment inAtt = new Messaging.InboundEmail.BinaryAttachment();


        email.subject = 'test unsubscribe test';
       email.fromName = 'abhi k';
       email.fromAddress = 'abhilash@rixyncs.co.in';
       // email.subject='test';
       email.plainTextBody='zsdzdf';

       
      Lead l = new lead(firstName='Rasmus',
                lastName='abc',
                Company='Salesforce',
                Email='rmencke@salesforce.com',LeadSource='OnlineEnquiry',Description='dsdfdgf'
                );
         insert l;
       Id aid=l.id;
    // Create a new test Contact and insert it in the Test Method 
       Contact c = new Contact(firstName='Rasmus',
                    lastName='Mencke',
                    Email='rmencke@salesforce.com');
       insert c;
      Attachment att=new Attachment(ParentId=aid,name='test',body=blob.valueof('test'));
      system.debug('attachgment'+ att.ParentId);
      insert att;
      // test with subject that matches the unsubscribe statement
  
    if(inAtt!=null){

      inAtt.body = blob.valueOf('test');
      inAtt.fileName = 'my attachment name';
      inAtt.mimeTypeSubType = 'plain/txt';
   
       email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] {inAtt}; 
        }
       // call the class and test it with the data in the testMethod
       unsubscribe unsubscribeObj = new unsubscribe();
       unsubscribeObj.handleInboundEmail(email, env);
                           
       }
       
      

}

 

 

Thanks

 

Plz help

MiddhaMiddha

Messaging.InboundEmail object gives you email address of the person who sent this email to the email service. That email might be forwarded multiple times. Messaging.InboundEmail wont give you information about that directly.

 

The options that you can try is to fethc the "headers" from Messaging.InboundEmail and parse it to check if the email is being forwaded and get the original email address (not sure but you can give it a try).

 

Another option you can try is to request the student forwarding the email, to add the email address of the original student in email subject. In your code, you can parse the subject and if an email exists, you can use that email address else the original from where the email is recieved.

JCNJJCNJ

Did you ever get this to work?