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:
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:
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.