EmailHandler-Salesforce


SUBMITTED BY: Guest

DATE: Jan. 22, 2014, 12:01 p.m.

FORMAT: Text only

SIZE: 2.1 kB

HITS: 4933

  1. global class CreateTaskEmailExample implements Messaging.InboundEmailHandler {
  2. global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
  3. Messaging.InboundEnvelope env){
  4. // Create an InboundEmailResult object for returning the result of the
  5. // Apex Email Service
  6. Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
  7. String myPlainText= '';
  8. // Add the email plain text into the local variable
  9. myPlainText = email.plainTextBody;
  10. // New Task object to be created
  11. Task[] newTask = new Task[0];
  12. // Try to look up any contacts based on the email from address
  13. // If there is more than one contact with the same email address,
  14. // an exception will be thrown and the catch statement will be called.
  15. try {
  16. Contact vCon = [SELECT Id, Name, Email
  17. FROM Contact
  18. WHERE Email = :email.fromAddress
  19. LIMIT 1];
  20. // Add a new Task to the contact record we just found above.
  21. newTask.add(new Task(Description = myPlainText,
  22. Priority = 'Normal',
  23. Status = 'Inbound Email',
  24. Subject = email.subject,
  25. IsReminderSet = true,
  26. ReminderDateTime = System.now()+1,
  27. Type__c='recieved',
  28. WhoId = vCon.Id));
  29. // Insert the new Task
  30. insert newTask;
  31. System.debug('New Task Object: ' + newTask );
  32. }
  33. // If an exception occurs when the query accesses
  34. // the contact record, a QueryException is called.
  35. // The exception is written to the Apex debug log.
  36. catch (QueryException e) {
  37. System.debug('Query Issue: ' + e);
  38. }
  39. // Set the result to true. No need to send an email back to the user
  40. // with an error message
  41. result.success = true;
  42. // Return the result for the Apex Email Service
  43. return result;
  44. }
  45. }

comments powered by Disqus