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
Lovely LopezLovely Lopez 

Create a Task on Contact or Open Opportunities for Email Inbound?

I'm trying to create a class that creates a task for an email inbound on a Contact level (but it would be really great if I can create it on Opportunity if you can help me figure it out). I'm currently testing this class on Contact and it isn't working. I'm not very well-versed with Apex. What am I doing wrong? Below is the Apex class I have.
global class CreateTaskEmail 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 Task object to be created
    Task[] newTask = new Task[0];
   
    // Try to look up any contacts based on the email from address
    // If there is more than one 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, AccountId
        FROM Contact
        WHERE Email = :email.fromAddress
        LIMIT 1];
        
      Account a = [SELECT Id, OwnerId FROM Account WHERE Id = :vCon.AccountId];
      
      // Add a new Task to the contact record we just found above.
      newTask.add(new Task(Description =  myPlainText,
           OwnerId = a.OwnerId,
           Record_Type__c = 'Normal Task',
           Priority = 'Normal',
           Status = 'Completed',
           Subject = 'Email: ' + email.subject,
           Type = 'Email Inbound',
           ActivityDate = System.today(),
           WhoId =  vCon.Id));
     
     // Insert the new Task 
     insert newTask;    
     
     System.debug('New Task Object: ' + newTask );   
    }
    // 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;
  }
}
I hope you can help me. Thank you so much!
cchenucicchenuci
Hi Lovely.

What is the use case that you are trying to implement?  We have an app currently in pilot that may be able to do this instead of coding but need some details on what you are trying to implement.

Thanks,
Christine
 
Lovely LopezLovely Lopez
Hi Christine,

We're trying to automatically create a task for an email inbound from Outlook. If a client or customer emails us or replied to our email, it will create a task to the person's account that has the email used and set the task type to email inbound.