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
ArikArik 

Insert Attachments to Email Service Class

Hi,

I need to add attachments to this class (Email)

Any ideas ?


global class tasks2 implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                  Messaging.InboundEnvelope env){

// Create an inboundEmailResult object for returning 
// the result of the Force.com Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

String myPlainText = '';

// Add the email plain text into the local variable

try
{
      myPlainText = email.plainTextBody.substring(0, email.plainTextBody.indexOf('<stop>'));
}
catch (System.StringException e)
{
     myPlainText = email.plainTextBody;
     System.debug('No <stop> in email: ' + e);
}

// new Task object to be created

Task[] newTask = new Task[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 Task to the contact record we just found above
 newTask.add(new Task(Description = myPlainText,
     Priority = 'Normal',
     Status = 'Inbound Email',
     Subject = email.subject,
     ActivityDate = Date.today(),
     IsReminderSet = false,
     ReminderDateTime = System.now()+1,
     WhoId = vCon.Id));

// Insert the new Task and it will be created and appended to the contact record
     insert newTask;

System.debug('New Task Object: ' + newTask );
}
   // If there is an exception with the query looking up
   // the contact this QueryException will be called.
   // and the exception will be written to the Apex Debug logs

   catch (System.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 Force.com Email Service
  return result;
}

static testMethod void testTasks() {

// Create a new email and envelope object
   Messaging.InboundEmail email = new Messaging.InboundEmail();
   Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

// Create the plainTextBody and fromAddres for the test
    email.plainTextBody = 'Here is my plainText body of the email';
    email.fromAddress ='rmencke@salesforce.com';

Tasks taskObj = new Tasks();
taskObj.handleInboundEmail(email, env);


}
kiranmutturukiranmutturu

try like this.....

if (email.binaryAttachments!=null && email.binaryAttachments.size() > 0) {
for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
objAttach = new Attachment(ParentId = strParentId,
Name = email.binaryAttachments[i].filename,
Body = email.binaryAttachments[i].body);

lstAttachment.add(objAttach);

--------------------------------------------------------------

//To process Text Attachments of type .txt
if (email.textAttachments!=null && email.textAttachments.size() > 0) {
objAttach = new Attachment(ParentId =strParentId,
Name = email.textAttachments[i].filename,
Body = Blob.valueOf(email.textAttachments[i].body) );
lstAttachment.add(objAttach);
..

 

finally insert the lstAttachment;

ArikArik

that looks like it will work,

 

However, forgive me, but when i put it in, it does not work.

 

Where exactly would you put it ?