+ Start a Discussion
Zoom_VZoom_V 

Can't get the attachment from the incoming email

I am attempting to use InboundEmailHandler to automatically create a new record (Vendor_Notes__c) from an email, associate the record with another record (Vendor_Profile__c) which is named in the Subject, and then take any attachment in the email and also associate it with that record Vendor_Profile__c record as well. 

I am able to create the new Vendor_Notes__c record and associate it with the Vendor_Profile__c. But I can't get the attachment to appear on the Vendor_Profile__c record. I don't know if the attachment portion of this class or if I need to set up something more for the Vendor_Profile__c object. I have a Notes & Attachments in it. I'm guessing that's all I would need.

Here is the class : 

 

global class VendorEmail implements 
        Messaging.InboundEmailHandler {

  
Vendor_Notes__c[] NewVendorNotes = new Vendor_Notes__c[0];

 global Messaging.InboundEmailResult handleInboundEmail(
  Messaging.InboundEmail email, 
  Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = 
        new Messaging.InboundEmailresult();
 
  // Captures email info
    
String emailAddress = envelope.fromAddress;
String fName = email.fromname.substring(
        0,email.fromname.indexOf(' '));
String lName = email.fromname.substring(
        email.fromname.indexOf(' '));
String[] emailBody = email.plainTextBody.split('\n', 0);
String phoneNumber = emailBody[0].substring(6);
       
//Associates email to proper Vendor Profile

Vendor_Profile__c venpro;
venpro = [select ID from Vendor_Profile__c where name =
             :email.subject limit 1];
    ID jobId = venpro.ID;
    

try
    {
      NewVendorNotes.add(new Vendor_Notes__c(Sender__c = emailAddress,
      Vendor__c = jobId,
      Routing_Number__c = lName,
     Account_Number__c = phoneNumber
      ));
 
      insert NewVendorNotes;
   }
    
   catch (System.DmlException e)   
   {
           

            //Save any Binary Attachment
            
            if (email.binaryAttachments != null)
                {
                    for(Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) 
                    {
                        Attachment attachment = new Attachment();
        
                        attachment.Name = bAttachment.fileName;
                        attachment.Body = bAttachment.body;
                        attachment.ParentId = venpro.Id;
                        insert attachment;
                    }
               } 
}
return result;
       
    }
}



Thank you for your help.

Best Answer chosen by Zoom_V
Vishal_GuptaVishal_Gupta
Hi,

You have written attachment related code in Catch section and thats mean attachment will create only if any DML exception will come. Please keep your attachment realted code in Try Section.

Thanks,
Vishal

All Answers

Vishal_GuptaVishal_Gupta
Hi,

You have written attachment related code in Catch section and thats mean attachment will create only if any DML exception will come. Please keep your attachment realted code in Try Section.

Thanks,
Vishal
This was selected as the best answer
Zoom_VZoom_V
That was indeed it - thank you sir !