You need to sign in to do that
Don't have an account?

Question while using InboundEmail Object
Hi all,
I was testing the InboundEmail functionality using Apex code and came across an issue. I'm using the sample code that came with the Spring '08 release as the squeleton to auto-generate a task when an email is received. This is how I've structured my code:
- The email.fromAddress will be used to query the Contact table and set the WhoId field of the task.
- Instead of assigning the task to whoever sent the email, I want to be able to control this by allowing the person that's sending the email to assigned a user to the task. So, what I do is within the body of the email, have a special character '#' right before the person's email address (which will become the Task's OwnerId). Then I split the email.plainTextBody when an '#' is found. For some reason, when I send an email, the task isn't created. If I hardcode the email address within the SOQL query, then the code works fine. Here's my code:
Code:
global class TaskFromEmail implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env) { Messaging.InboundEmailResult emailResult = new Messaging.InboundEmailResult(); String myPlainText = ''; myPlainText = email.plainTextBody; Task[] newTask = new Task[0]; String[] userEmailAddress = new String[2]; User myUser = null; if (myPlainText.contains('#')) { userEmailAddress = myPlainText.split('#', 2); System.debug('##### ' + userEmailAddress[1] + ' #####'); } try { Contact myContact = [SELECT Id, Name, Email FROM Contact WHERE Email = :email.fromAddress LIMIT 1]; myUser = [SELECT Id, Email FROM User WHERE Email = :userEmailAddress[1] //WHERE Email = :email.subject LIMIT 1]; newTask.add(new Task(Description = userEmailAddress[0], Priority = 'Normal', Status = 'Inbound Email', Subject = email.subject, OwnerId = myUser.Id, ActivityDate = System.today()+1, WhoId = myContact.Id)); insert newTask; System.debug('New Task Object: ' + newTask); } catch (QueryException e) { System.debug('Contact Query issue: ' + e); } emailResult.success = true; return emailResult; } static testMethod void testTasks() { 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 #myEmailAddress@someDomain.com'; email.fromAddress ='myEmailAddress@someDomain.com'; TaskFromEmail taskObj = new TaskFromEmail(); taskObj.handleInboundEmail(email, env); } }
Does anyone have an idea? From what I've seen, is the issue that I'm passing a variable (userEmailAddress[1]) into an SOQL query string?
Message Edited by J&A-Dev on 02-20-2008 12:34 PM
for instance, if this was the email sent
string[0] = "Hi bob\, here is my email address";
string[1] = "email\@domain.com <two line breaks> Have a nice day\!";
right?
(btw, I'm no expert, just trying to lend a hand here while the Eclipse toolkit is being fixed, since it isn't allowing deployments to non-dev orgs afaik at the moment)
Message Edited by paul-lmi on 02-20-2008 01:35 PM