[SalesForce] Need to ingest email to a custom object

I need to ingest email via the Email-to-Salesforce feature and then look up the sender from their email address (from) in a custom object in order to set a "responded" flag in their record. The custom object is client__c and (for now) I am just trying to get the following to compile.

Here is the code for an email service I've been working with.

// Create an inboundEmailResult object for returning the result of the Apex Email Service

global class ProcessInboundSMS implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope) {

  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 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 Client__c
    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,
       IsReminderSet = true,
       ReminderDateTime = System.now()+1,
       WhoId =  vCon.Id));

        // Insert the new Task 
        insert newTask;

 }  catch (Exception e) {
    }

return null;
 }
 }

Best Answer

Update:

The error you're getting with your current code is likely from this line:

Contact vCon = [Select Id, Name, Email
    From Client__c
    Where Email = :email.fromAddress
    Limit 1];

It's trying to select a Client__c and store it as a Contact object. You'll also probably run into an error with the email field because there it'll be a custom field so it, too, will need the __c at the end. Try changing it to:

   Client__c vCon = [Select Id, Name, Email__c
        From Client__c
        Where Email__c = :email.fromAddress
        Limit 1];

Original

Email to Salesforce only automatically associates with Leads, Contacts, and Opportunities.

Doing this with a custom object would require an email service. This lets you take in email and then process it with Apex. You could add your logic there.

The docs have a sample showing how to create a task for contacts from an email. You could adapt this to your needs.

global class CreateTaskEmailExample 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
        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,
           IsReminderSet = true,
           ReminderDateTime = System.now()+1,
           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;
  }
}
Related Topic