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
StaciStaci 

inbound email handler with attachments

I have an email handler creating cases from a certain email coming in and they contain attachments.  If the attachment is in the body of the email the attachment works fine, but for some reason some of them come in a different way and the attachment is in an "attachment" section of the email and they get dropped off.  Is there something else I need to add to get these other attachments to add to the case?
Blake TanonBlake Tanon
Can you post your handler class(s) and a part of your debug log where it shows the attachment missing?
StaciStaci
30.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
08:26:43.052 (52234000)|EXECUTION_STARTED
08:26:43.052 (52280000)|CODE_UNIT_STARTED|[EXTERNAL]|01pe0000000DcDt|CW_alertEmailHandler.handleInboundEmail
08:26:43.056 (56111000)|METHOD_ENTRY|[1]|01pe0000000DcDt|CW_alertEmailHandler.CW_alertEmailHandler()
08:26:43.056 (56125000)|METHOD_EXIT|[1]|CW_alertEmailHandler
08:26:43.056 (56293000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
08:26:43.056 (56339000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
08:26:43.060 (60199000)|SYSTEM_METHOD_ENTRY|[12]|String.substringAfter(String)
08:26:43.060 (60247000)|SYSTEM_METHOD_EXIT|[12]|String.substringAfter(String)
08:26:43.060 (60411000)|DML_BEGIN|[16]|Op:Insert|Type:Case|Rows:1
StaciStaci
global class CW_alertEmailHandler implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Case c;
     
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
         
      try
      {
      c = new Case();
      c.recordTypeId = '012f00000000JLc';
      c.OwnerId = '00G30000001oUSL';
      c.Site_Code__c = email.subject.substringAfter('##');
      c.AccountId = '0013000000aRZ67';
      c.Description = email.plainTextbody;
      c.Subject = email.subject;
      insert c;
      //to save attachment to case
      for(Messaging.Inboundemail.TextAttachment tAttachment:
      email.textAttachments) {
          Attachment attachment = new Attachment();
          attachment.Name = tAttachment.fileName;
          attachment.Body = Blob.valueOf(tAttachment.body);
          attachment.ParentId = c.Id;
          insert attachment;
      }

     
      result.success = true;
      }catch (Exception e){
      result.success = false;
      result.message = 'Oops, I failed.' + e.getMessage();
      }
          return result;
  }}
StaciStaci
Anyone have any advice?
Blake TanonBlake Tanon
If you don't find an attachment as text - then move to .binaryAttachment.

Also you should add your attachments to a list and insert them outside of your loop - this will save on DML limits and system resources.
StaciStaci
Thanks Blake, I added the list and insert them outside the loop and I tried the binary route, still didn't find the attachment in the "Attachment" section of the email, see the pic below.  If the email has the attachment section like this, the file doesn't attach in salesforce.  If the email is physically in the body of the email it works fine.  Any other ideas?
User-added image