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
LaurenP6777LaurenP6777 

Test Method Help for Inbound Email Handler

Hi All, 

I created the Email Handler class below to create a new task on a custom object (User_Support__c). The email will link to the appropriate record by matching the email subject to an auto-number field (Support__c) on the custom object. Unfortunately, I can't get the test method to work. I continually get an error that their are no rows for assignment. Can anyone tell me what I am doing wrong? 


/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * email.
 */
global class EmailtoSalesForce implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

        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 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.
  
   User_Support__c vUS = [Select Id, Support__c, Name
    From User_Support__c
    Where Support__c = :email.subject
    Limit 1];
  
  // Add a new Task to the contact record we just found above.
  newTask.add(new Task(Description =  myPlainText,
       Priority = 'Normal',
       Status = 'Completed',
       Type = 'Email',
        Subject = email.subject,
        IsReminderSet = true,
        ReminderDateTime = System.now()+1,
        WhatId =  vUS.Id));
 
 // Insert the new Task 
 insert newTask;
 

                if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
        Attachment attachment = new Attachment();
        // attach to the newly created contact record
        attachment.ParentId = vUS.Id;
        attachment.Name = email.binaryAttachments[i].filename;
        attachment.Body = email.binaryAttachments[i].body;
        insert attachment;
    
            }
        }

    result.success = true;
        return result;
    }
    }
}


TEST CLASS
@isTest
private class testusersupportattach{

static testMethod void testMe() {

 Profile p = [Select id from Profile where name = 'GCP'];


User u = new User(alias = 'mo123', email='mousey.1234@covance.com',
      emailencodingkey='UTF-8', lastname='123', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p.Id, country='United States',
      CVD_Division__c='ED', User_Type__c='AE',
      timezonesidkey='America/Los_Angeles', username='mousey.1234@covance.com');
  insert u;
  
 
  
User_Support__c us= New User_Support__c(name='attach email to User Support Case',
    Ideal_completion_date__c=System.Today(),
    Request_Type__c='Development',
    Priority_Level__c='Low',
    RecordTypeId='012C0000000I7wx',
    Requested_By__c=u.id);
    
    insert us;
    
    
    
    Test.StartTest();

  // create a new email and envelope object
  Messaging.InboundEmail email = new Messaging.InboundEmail() ;
  Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

  // setup the data for the email
  email.subject = us.support__c;
  email.fromname = 'FirstName LastName';
  env.fromAddress = u.email;
  

  // call the email service class and test it with the data in the testMethod
  EmailtoSalesForce emailProcess = new EmailtoSalesForce();
  emailProcess.handleInboundEmail(email, env);
Test.StopTest();

  // query for the contact the email service created
  Task Tsk = [select id, Type, status, subject, whatid from Task
    where whatid =:us.id];

  System.assertEquals(Tsk.Subject,us.name);
  System.assertEquals(Tsk.Status,'Completed');
  System.assertEquals(Tsk.Type,'Email');

 
}
}
Best Answer chosen by LaurenP6777
jp1234jp1234
Can you replace "insert us;" statment in the test class with "Database.insert(us);"?  insert DML definitely doesn't populate the fields within the original object with auto-generated value.  Database.insert statement does populate the ID field on the original object after execution, but not sure about the autonumber field.  If that doesn't work, you might have to try something like following:
 
Database.insert(us);

User_Support__c insertedUs = [SELECT Support__c, ...
                    FROM User_Support__c WHERE ID = :us.Id];

...
email.subject = insertedUs.Support__c;
...

 

All Answers

jp1234jp1234
Can you replace "insert us;" statment in the test class with "Database.insert(us);"?  insert DML definitely doesn't populate the fields within the original object with auto-generated value.  Database.insert statement does populate the ID field on the original object after execution, but not sure about the autonumber field.  If that doesn't work, you might have to try something like following:
 
Database.insert(us);

User_Support__c insertedUs = [SELECT Support__c, ...
                    FROM User_Support__c WHERE ID = :us.Id];

...
email.subject = insertedUs.Support__c;
...

 
This was selected as the best answer
LaurenP6777LaurenP6777
This absolutely did the trick! Thank you so much!!!