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

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:
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] + ' #####');
   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 =,
        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';
  email.fromAddress ='';

  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?
i believe you need to precede Apex variables with a colon (:) when passing them into an SOQL query.
That's correct and that's what I do (precede the Apex variable with a : ). My code compiles and runs, but the task is never created.
The strange thing is that when I run the testPlan for this code, I get an 89% code coverage and I don't see any errors while executing the SOQL queries. At first glance, I thought user created variables were not allowed within Apex SOQL query strings, but my code compiles.
what does your debug line actually output in your code that does the string split?

My debug line outputs the email address as expected (the email address is embedded within the email's body).
Assuming that the person that sends the email only has one user to assign the task to, my array userEmailAddress should only contain 2 values: one string containing all the characters until it finds the '#' character (userEmailAddress[0]) and another string containing the email address within the email's body (userEmailAddress[1]).

Message Edited by J&A-Dev on 02-20-2008 12:34 PM
string.split will return the embedded email, and the rest of the email body the way you've implemented it.  the only way this would be perfectly successful is if was the absolute last text in the email when it's sent.

for instance, if this was the email sent

Hi bob,

Here is my email address

Have a nice day!

string.split using the # as the expression would return

string[0] = "Hi bob\, here is my email address";
string[1] = "email\ <two line breaks> Have a nice day\!";


(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
Agreed and I appreciate your input. Yes, for testing purposes, I'm sending the email and ensuring that the last text portion of the email body is indeed a valid email (my work email). I even printed out the 2nd value of the split function to ensure that I was only getting back the email address and nothing else. On my debug log, I only see the email address (when I run the testClass), but when I actually send an email, no task is generated from my email.
I haven't yet deployed any Apex triggers, but from reading the posts on this forum, it seems a good number of people are having issues.
the only issue i've had (besides the fact that i can't deploy to my production org) was that the checkbox for Active defaults to being unchecked.  You actually have to login to the account and enable it manually as far as I can see, and, when you create the email address, it wants to only allow your address to send to it unless you remove that limitation by blanking the allowed senders list.
Rasmus MenckeRasmus Mencke
I would add a couple more debug statements. Verify that your query returns any values - maybe check if the email.fromAddress is coming in mixed case.

Thanks for the response Rasmus.
I added debug statements after each query (one after querying the Contact table and another after the User table) printing the contact id and user id respectively, and they do get printed out fine.
Finally got it to work.
I ended up adding the trim() function before the split to get rid of any trailing characters, and all is well now.
Thanks to everyone for the input.