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
yespeeyespee 

case attachment

New to Apex and I wrote an apex class for email to case based on some sample code. Everything works fine but does not work when there  is an attachment in the mail. Here is my code. I believe I am making some small mistake with this. Can some one guide me through please. 

Code:
global class CreateSupportCase 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 myPlainText= '';
  
  // Add the email plain text into the local variable 
 
  myPlainText = email.plainTextBody;
 
  // New Case object to be created
  
  Case[] newCase = new Case[0];
 
  // Try to lookup any contacts based on the email from address
  // If there is more than 1 contact with the same email address,
  // an exception will be thrown and the catch statement will be called.
  try {
  Contact vCon = [Select Id, Name, Email
    From Contact
    Where Email = :email.fromAddress
    Limit 1];
  
  // Add a new Case to the contact record we just found above.
  newCase.add(new Case(Description =  myPlainText,
       Priority = 'Normal',
       Status = 'New',
       Subject = email.subject,
       Origin = 'Email',
       SuppliedEmail =  email.fromAddress,
       contact= vCon));
 
 // for email attachments
 
 if (email.binaryAttachments!=null && email.binaryAttachments.size() > 0) {
             for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
 
 System.debug('Binary Attachments - filename: ' + email.binaryAttachments[i].filename);
 System.debug('Binary Attachments - size: ' +   email.binaryAttachments[i].mimeTypeSubType);
            
             Attachment a = new Attachment(ParentId = newCase[0].Id,
                                           Name = email.binaryAttachments[i].filename,
                                           Body = email.binaryAttachments[i].body);
             insert a;
             }
    }
 
 
 
 // Insert a new Case 
 insert newCase;
  
 
 System.debug('New Case Object: ' + newCase );
 
  }
  // If an exception occurs when the query accesses 
  // the contact record, a QueryException is called.
  // The exception is written to the Apex debug log.

 catch (QueryException e) {
  System.debug('Query Issue: ' + e);
 }
 
 // Set the result to true. No need to send an email back to the user 
 // with an error message
 
    result.success = true;
 
 // Return the result for the Apex Email Service
    return result;
 
  }
 }

 
Thanks in advance