+ Start a Discussion
Jonathan OsgoodJonathan Osgood 

Inbound Email Handler Attachment

Hi Everyone,

Implementing a basic email handler to email an attachement and have it save as an account attachment (in the notes and attachment related list). I am able to create a new account, but not match and update an existing and most importantly, the email attachment does not land on the account reocrd. I beleive my email services setting are OK (set to accept All attachements). Here is my class:
 
global class EmailToSalesForce implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                         Messaging.Inboundenvelope envelope) {
  
Account account;
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

try {



// Look for account whose name is the subject and create it if necessary
if ([select count() from Account where Name =:email.subject] == 0) {
  account = new Account();
  account.Name = email.subject;
  insert account;
} else {
  account = [select Id from Account where Name =:email.subject];
}



// Save attachments, if any
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
  Attachment attachment = new Attachment();
 
  attachment.Name = tAttachment.fileName;
  attachment.Body = Blob.valueOf(tAttachment.body);
  attachment.ParentId = account.Id;
  insert attachment;
}
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
  Attachment attachment = new Attachment();
 
  attachment.Name = bAttachment.fileName;
  attachment.Body = bAttachment.body;
  attachment.ParentId = account.Id;
  insert attachment;
}
      
      result.success = true;
      result.message = (email.subject + ' matched');
    } catch (Exception e) {
      result.success = false;
      result.message = 'Oops, I failed.';
    }
   
    return result;
  }
}

 
Best Answer chosen by Jonathan Osgood
Vishal_GuptaVishal_Gupta
Hi Jonathan,

I have found one flaw in code, please use the below code :
global class EmailToSalesForce implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                         Messaging.Inboundenvelope envelope) {
  
Account account;
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

try {

        // Look for account whose name is the subject and create it if necessary
        if ([select count() from Account where Name =:email.subject] == 0) {
          account = new Account();
          account.Name = email.subject;
          insert account;
        } else {
          account = [select Id from Account where Name =:email.subject];
        }
        
        if(email.textAttachments != null)
        {
            // Save attachments, if any
            for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
              Attachment attachment = new Attachment();
             
              attachment.Name = tAttachment.fileName;
              attachment.Body = Blob.valueOf(tAttachment.body);
              attachment.ParentId = account.Id;
              insert 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 = account.Id;
              insert attachment;
            }
        }
      result.success = true;
      result.message = (email.subject + ' matched');
    } catch (Exception e) {
      result.success = false;
      result.message = 'Oops, I failed. Error : '+e.getMessage();
    }
   
    return result;
  }
}
I have put condition before looping the text attchments and Binary attachments because there might be chance that only one type of attachment come through email, in that case other one give null pointer exception.

Please let me know if it will work for you.

Thanks,
Vishal

All Answers

Vishal_GuptaVishal_Gupta
Hi Jonathan,

I have found one flaw in code, please use the below code :
global class EmailToSalesForce implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                         Messaging.Inboundenvelope envelope) {
  
Account account;
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

try {

        // Look for account whose name is the subject and create it if necessary
        if ([select count() from Account where Name =:email.subject] == 0) {
          account = new Account();
          account.Name = email.subject;
          insert account;
        } else {
          account = [select Id from Account where Name =:email.subject];
        }
        
        if(email.textAttachments != null)
        {
            // Save attachments, if any
            for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
              Attachment attachment = new Attachment();
             
              attachment.Name = tAttachment.fileName;
              attachment.Body = Blob.valueOf(tAttachment.body);
              attachment.ParentId = account.Id;
              insert 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 = account.Id;
              insert attachment;
            }
        }
      result.success = true;
      result.message = (email.subject + ' matched');
    } catch (Exception e) {
      result.success = false;
      result.message = 'Oops, I failed. Error : '+e.getMessage();
    }
   
    return result;
  }
}
I have put condition before looping the text attchments and Binary attachments because there might be chance that only one type of attachment come through email, in that case other one give null pointer exception.

Please let me know if it will work for you.

Thanks,
Vishal
This was selected as the best answer
Jonathan OsgoodJonathan Osgood
Thank you, Vishal! That solved it!