• JCNJ
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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

  • March 17, 2011
  • Like
  • 0