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 with Attachments

Hi,

 

I am trying to create  an Email to lead.i mean when i use the email service link to send the mail to SF, a lead is generated.As this is working ,but when i am trying to attach a file with the Email.The file is not comin as attachments.My code is under.Can any one try to solve my problem.

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;
    String strSubject = email.subject;
    String myText=email.plainTextBody;
    String myFromName = email.fromName;
    Lead l;
    
       

    //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)
    {   
        l = new Lead(
        lastName= myFromName, //strEmailId,
        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 l;    
       
    }
   
   
    result.success = true;
    return result;
    for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
     Attachment attachment = new Attachment();
     
    attachment.Name = tAttachment.fileName;
     attachment.Body = Blob.valueOf(tAttachment.body);
     attachment.ParentId = l.Id;
     insert attachment;
    }
    for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
     Attachment attachment = new Attachment();
     
    attachment.Name = bAttachment.fileName;
     attachment.Body = bAttachment.body;
      attachment.ParentId =l.Id;
     insert attachment;
   
    }
     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();
      
        // Create a new test Lead and insert it in the Test Method       
       Lead l = new lead(firstName='Rasmus',
                lastName='abc',
                Company='Salesforce',
                Email='rmencke@salesforce.com',LeadSource='OnlineEnquiry',Description='dsdfdgf'
                );
       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');
       insert c;
      
       // test with subject that matches the unsubscribe statement
       email.subject = 'test unsubscribe test';
       email.fromName = 'abhi k';
       email.fromAddress = 'abhilash@rixyncs.co.in';
        email.subject='test';
       email.plainTextBody='zsdzdf';
        
       
       
  
       // call the class and test it with the data in the testMethod
       unsubscribe unsubscribeObj = new unsubscribe();
       unsubscribeObj.handleInboundEmail(email, env );
                           
       }
       
      /* static testMethod void testTrigger() {
        Lead l = new Lead(lastName='testTrigger',Status='Qualified',Email='abhi@rixyncs.co.in',
                          Company='Rixyncs',MobilePhone='919901379361');
        insert l;
      //  SMS__c s = new SMS__c(To_Lead__c=l.Id,Message__c='Test');
      //  insert s; 
        
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(l.Id);
        lc.setConvertedStatus('Qualified');
        Database.LeadConvertResult lcr = Database.convertLead(lc);
    }*/

}

 Please help me.

 

Thanks

 

Nasir

 

SfdcBlueFishSfdcBlueFish

Hi Nasir,

 

i think u r tring , if inbound email contians any attchment then it will attches to newly created Lead... so please 

check with this code

 

if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {

      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {

        Attachment attachment = new Attachment();

        // attach to the newly created Lead record

        attachment.ParentId =l.id;

        attachment.Name = email.binaryAttachments[i].filename;

        attachment.Body = email.binaryAttachments[i].body;

        insert attachment;

      }

    }

NasirNasir

Hi

 

I tried to edit my code with yours with the binary attachments code.But it did't worked.I had send the mail with an attachment but attchements did't appear.Here is the modified code.

 

Well one more thing binary attachment is to attach image,vedio and textAttachments is for .vcf and .vcs extension.

 

and in my mail i am sending a .doc file.

here is the mdified code

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;
    String strSubject = email.subject;
    String myText=email.plainTextBody;
    String myFromName = email.fromName;
    Lead l;
    
       

    //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)
    {   
        l = new Lead(
        lastName= myFromName, //strEmailId,
        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 l;    
       
    }
   
   
    result.success = true;
    return result;
    
    if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {

      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {

        Attachment attachment = new Attachment();

        // attach to the newly created Lead record

        attachment.ParentId =l.id;

        attachment.Name = email.binaryAttachments[i].filename;

        attachment.Body = email.binaryAttachments[i].body;
        System.debug('nasir attachments:'+ attachment); 

        insert attachment;

      }

    }
    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();
      
        // Create a new test Lead and insert it in the Test Method       
       Lead l = new lead(firstName='Rasmus',
                lastName='abc',
                Company='Salesforce',
                Email='rmencke@salesforce.com',LeadSource='OnlineEnquiry',Description='dsdfdgf'
                );
       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');
       insert c;
      
       // test with subject that matches the unsubscribe statement
       email.subject = 'test unsubscribe test';
       email.fromName = 'abhi k';
       email.fromAddress = 'abhilash@rixyncs.co.in';
        email.subject='test';
       email.plainTextBody='zsdzdf';
        
       
       
  
       // call the class and test it with the data in the testMethod
       unsubscribe unsubscribeObj = new unsubscribe();
       unsubscribeObj.handleInboundEmail(email, env );
                           
       }
       
      /* static testMethod void testTrigger() {
        Lead l = new Lead(lastName='testTrigger',Status='Qualified',Email='abhi@rixyncs.co.in',
                          Company='Rixyncs',MobilePhone='919901379361');
        insert l;
      //  SMS__c s = new SMS__c(To_Lead__c=l.Id,Message__c='Test');
      //  insert s; 
        
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(l.Id);
        lc.setConvertedStatus('Qualified');
        Database.LeadConvertResult lcr = Database.convertLead(lc);
    }*/

}

 

 

IvarIvar

I am having a similar problem, I am trying to attach two incoming attachments to a Lead record, one textAttachment (vcf file) and one binaryAttachment (jpeg image).

 

The text one works fine, but it seems the email.binaryAttachments list is always empty. Has anyone got a solution for this?

 

 

List<Attachment> attachments = new List<Attachment>();
        	
        	//add  binaries
        	try {
	        	for( Messaging.InboundEmail.BinaryAttachment bin: email.binaryAttachments){
	        		Attachment a = new Attachment();
	        		a.ParentId = l.Id;
	        		a.Name = bin.filename;
	        		a.Body = bin.body;
	        		attachments.add( a );
	        	}
        	}
        	catch( Exception e ){}
        	 
        	//add texts
        	try{
	        	for( Messaging.InboundEmail.TextAttachment txt: email.textAttachments){
	        		Attachment a = new Attachment();
	        		a.ParentId = l.Id;
	        		a.Name = txt.filename;
	        		a.Body = Blob.valueOf(txt.body);
	        		attachments.add( a );
	        	}
        	}
        	catch( Exception e ){}
        	 
        	try{
        		insert attachments;
        	}
        	catch( Exception e ){
        		throw e;
        	}