I've successfully set up an inbound email to load a particular set of data, however when writing the test case i'm getting the invalid type error on the following line:

EmailDemoReceive edr = new EmailDemoReceive();

Any ideas?

2 Answers 2


a bit of googling... Call the InboundEmail class rather than EmailDemoReceive


This is my implemented class

global class SFA_EmailServiceHandlerUtility implements Messaging.InboundEmailHandler{

//Apex classes that implement the Messaging.InboundEmailHandler interface
//Inbound email handler to intercept the email and start the next batch
//This class compares the body string passed in the Email Handler Utility Class  and start the next job
//This class contains business logic for chaining of two Jobs ,Compare the strings and start the next job based on string passed in the Email_Handler Class.

      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope envelope){
        system.debug('Start of handleInboundEmail method');
        if (email.plainTextBody==Label.SFA_BatchStartOrder){
              // create the instance of second batch class
              String query = 'Select Winner__c, Loser__c, Processed__c, Error__c from Address_Merge__c' 
                     + ' where Processed__c = false and Error__c = false';
              SFA_AddressMergeBatch addMerge = new SFA_AddressMergeBatch(query);
              Id batchprocessId = Database.executeBatch(addMerge);
      Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
      system.debug('End of handleInboundEmail method');
      return result;

This is my test class for same


private class SFA_TestEmailServiceHandlerUtility {

//Test case 1-Positive Scenario 

/*Aim:  Test whether second batch is triggered if body has correct specified message

private  static testMethod void testEmailHandler(){

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

    //Set up the Test data

     Account winAccount = new Account();
     winAccount.Name ='TestFirst TestSecond';
     insert winAccount;

    Address__c winAddress = new Address__c();
    winAddress.AMDM_Address_ID__c ='4011';
    winAddress.Address_Line_1__c='Win Address Line 1';
    insert winAddress;

    Address__c loseAddress = new Address__c();
    loseAddress.AMDM_Address_ID__c ='5011';
    loseAddress.Address_Line_1__c='Lose Address Line 1';
    insert loseAddress;

    Address_Merge__c addMerg= new Address_Merge__c ();
    addMerg.winner__c ='4011';
    addMerg.loser__c ='5011';
    insert addMerg;

        // Create the email body
        email.plainTextBody = Label.SFA_BatchStartOrder;
        email.fromAddress =SFA_EmailServiceAddress__c.getInstance('SFA_EmailServiceHandler').EmailAddress__c;
        email.subject = 'Test Batch';

        SFA_EmailServiceHandlerUtility edr = new SFA_EmailServiceHandlerUtility();

        Messaging.InboundEmailResult result = edr.handleInboundEmail(email, env);
        System.assertEquals(result.success, true);

        List <Address__c> reparentedAddress = [SELECT id  FROM Address__c where id=:loseAddress.id];//Verify that the loser Id record  is deleted

        System.AssertEquals(0,reparentedAddress.Size());  //Assert whether the loser address is deleted .This confirms batch ran



watch clearly the way i have instantiated the email handler class .

Hence We must instantiate the email handler first then pass the envelop and email in handleinbound email method

You must log in to answer this question.

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