I am trying to create an Apex class which will add the incoming email message which contains projectID in the subject heading into an existing project(custom objec) and discard the email message if it doesn't match any of our project ID.

The test class works fine but is not able to trigger the true condition for the below IF statement even though the System.debug(myMatcher.group()); contains a valid Name in the project__c database.

IF ([SELECT COUNT() FROM project__c WHERE Name =:myMatcher.group()] == 1)

I have ran a similar Apex code as below below using developer console (Open Execute Anonymous Window) and the three system debugs are returning TRUE result of the IF statement:


Whereby running the test class returns below DEBUG results:

USER_DEBUG [58]|DEBUG|No Match Found

My custom email handler class is as below

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

Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

//Set the Regex Matching Pattern to look for Project number in the email subject heading
String EmailSubject = email.subject;
String regexMatch = '^(UKGWY\\d+\\b?)';
//Declare a new instance of the Project object
project__c p;

try {

    // Check to see if the email subject contains any project ID
    Pattern MyPattern = Pattern.compile(regexMatch);
    Matcher MyMatcher = MyPattern.matcher(EmailSubject);
    //Condition if there is exactly one project ID match from email subject
    if ([SELECT COUNT() FROM project__c WHERE Name =:myMatcher.group()] == 1) {
    p = [SELECT ID FROM project__c WHERE Name =:myMatcher.group()];
    Note note = new Note();
    note.Title = email.fromName + '(' +DateTime.now() + ')';
    note.Body = email.plainTextBody;
    note.ParentId = p.Id;
    insert note;

      // Save attachments, if any 
      if (email.binaryAttachments!=null && email.binaryAttachments.size() > 0) { 
        for (Messaging.Inboundemail.BinaryAttachment bAttachment :email.binaryAttachments) { 
        Attachment attachment = new Attachment(); 
        attachment.ContentType = bAttachment.mimeTypeSubType; 
        attachment.Name = bAttachment.fileName; 
        attachment.Body = bAttachment.body; 
        attachment.ParentId = p.Id; 
        insert attachment; 

      else if (email.textAttachments!=null && email.textAttachments.size() > 0) { 
        for (Messaging.Inboundemail.TextAttachment tAttachment :email.textAttachments) { 
        Attachment attachment = new Attachment(); 
        attachment.Name = tAttachment.fileName; 
        attachment.Body = Blob.valueOf(tAttachment.body); 
        attachment.ParentId = p.Id; 
        insert attachment; 

    //Condition if there are more than one project ID match from email subject
    else if ([SELECT COUNT() FROM Project__c WHERE Name =:myMatcher.group()] > 1) {
    system.debug('More than 1 project ID found');
    } else {
      system.debug('No Match Found');

    result.success = true;
    } catch (Exception e) {
      result.success = false;
      result.message = 'Oops, I failed.';

    return result;

My test class is as below:

public class CI_EmailToSalesForce_Test {
    static testMethod void testCI_EmailToSalesForce() {
        // Create a new email, envelope and attachment object
        Messaging.InboundEmail email  = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        Messaging.InboundEmail.BinaryAttachment att = new Messaging.InboundEmail.BinaryAttachment();

        // Setup the data for the email
        email.subject = 'UKGWY0000000002 This is a test';
        email.plainTextBody = 'This should become a note';
        env.fromAddress ='[email protected]';
        String contactEmail = '[email protected]';
        att.body = blob.valueOf('test attachment');
        att.fileName = 'testfile.txt';
        att.mimeTypeSubType = 'text/plain';

        email.binaryAttachments = new Messaging.InboundEmail.binaryAttachment[] {att};

        // Call the TSG_Email class and test this with the above data
        CI_EmailToSalesForce CI_EmailObjTst = new CI_EmailToSalesForce();

        CI_EmailObjTst.handleInboundEmail(email, env);


​Can anyone tell me what I have done wrong?

Kind regards, Chris

1 Answer 1


This is a very silly mistake. By default test classes follow (SeeAllData= False) condition.

What does this mean ? It means that all methods executing in the test class will not have access to your org data.

Solution: You will have to create a sample Project record in your test class with same project Number which you are passing in the email subject.

You can find more details here https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm

  • Thank you Sir. That help me solve the problem. Sorry to be a newbie in this, am still new to Salesforce development. :-)
    – Apple
    Commented Feb 2, 2016 at 0:50
  • Glad you were able to solve this. Cheers Commented Feb 2, 2016 at 0:55
  • Yes, able to solve it by letting the test class see all data which I believed is not a good practice. However I am unable to insert projectID (Name) in the custom object as it is an auto number. The insert solution works fine if I changed it from autonumber to text but the requirement for this field is auto number.
    – Apple
    Commented Feb 2, 2016 at 1:36
  • Why don't you just create a product, Query its name(auto number) and then pass this value in the email subject? Commented Feb 2, 2016 at 1:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .