-1

I'm struggling with the following problem for two days. I want to create a test class for my apex class. here is code of the apex class

global class Email2Lead implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
                                                                Messaging.InboundEnvelope env) {

        // Create an InboundEmailResult object for returning the result of the
        // Apex Email Service       
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

        //use default assignment rule
        Database.DMLOptions dmo    = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule = true;

        try {    
            //select lead
            Lead k = this.get_recent_lead(email.fromAddress);

            //select contact
            Contact c = this.get_contact(email.fromAddress);

            //brand new mail?
            //Boolean first_touch = (k != null) & (c != null);

            //not a contact, not a lead? create a new one!
            //if (k != null && c != null) {

                //create new lead
                k = new Lead(
                    firstname                       = this.getContactFirstname(email.fromName),
                    lastname                            = this.getContactLastname(email.fromName),
                    company                             = this.getContactCompany(email.fromAddress),
                    email                               = email.fromAddress,
                    //email_origin__c             = env.fromAddress,
                    leadsource                      = 'bant.io',
                    Description =                 email.plainTextBody
                    //lead_source_detail__c = 'joinus@wework.com'
                );

                //use lead assignment rules
                k.setOptions(dmo);

                //dml
                insert k;

                //re-query
                k = [   SELECT id, ownerId, isConverted, convertedContactId, convertedAccountId 
                            FROM Lead 
                            WHERE id = :k.id 
                            LIMIT 1
                        ];
           // }

            //collect what
            Id whatId = null;           



        }
        catch(Exception pEx) {
            result.success = false; //default value
            system.debug(pEx.getMessage());
            throw new Email2LeadException(pEx.getMessage());
        }

        return result;
    }


    private Lead get_recent_lead(String pEmail) {       
        Lead[] Ks = [   SELECT id, email, isConverted, convertedContactId, convertedContact.ownerId, ownerId, convertedAccountId 
                                    FROM Lead 
                                    WHERE email = :pEmail 
                                    ORDER BY createddate DESC
                                    LIMIT 1
                                ];

        return Ks.size() > 0 ? Ks[0] : null;
    }


    private Contact get_contact(String pEmail) {        
        Contact[] Ks = [    SELECT id, AccountId, ownerId 
                                            FROM Contact 
                                            WHERE email = :pEmail 
                                            ORDER BY createddate DESC
                                            LIMIT 1
                                        ];

        return Ks.size() > 0 ? Ks[0] : null;
    }



    private String getContactFirstname(String pFrom) {      
        String result;
        try {
            result = pFrom.substring(0,pFrom.indexOf(' '));  
        }
        catch(Exception pEx) {
            result = ''; //default value
        }

        return result;
    }


    private String getContactLastname(String pFrom) {
        String result;
        try {
            result = pFrom.indexOf(' ') > 0 ? 
                                pFrom.substring(pFrom.indexOf(' ')) :
                                pFrom;
        }
        catch(Exception pEx) {
            result = ''; //default value
        }

        return result;
    }


    private String getContactCompany(String pEmail) {
        String result;
        try {
            result = pEmail.indexOf('@') > 0 ? 
                                pEmail.substring(pEmail.indexOf('@') + 1) :
                                pEmail;
        }
        catch(Exception pEx) {
            result = ''; //default value
        }

        return result;
    }


    public class Email2LeadException extends Exception{}
}

I am new to development and to salesforce. Anybody can help me out?

1
  • What have you tried so far? Maybe include your existing test class and we can take it from there.
    – Nick Cook
    Nov 20, 2017 at 6:06

1 Answer 1

2

You can use the below test class.

@isTest
public class Email2LeadTest {
static testMethod void TestinBoundEmail()
{
  Messaging.InboundEmail email = new Messaging.InboundEmail() ;
  Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
  // setup the data for the email
  email.subject = 'Test Data';
  email.fromname = 'FirstName1 LastName1';
  email.fromAddress = 'test@gmail.com';
  env.fromAddress = 'test@gmail.com';
  // call the email service class and test it with the data in the testMethod
  Email2Lead emailProcess = new Email2Lead ();
  emailProcess.handleInboundEmail(email, env);
}
}

For more help refer below reference link.

http://blog.jeffdouglas.com/2010/03/12/writing-an-inbound-email-service-for-salesforce-com/

http://www.srinivas4sfdc.com/2013/11/test-class-example-for.html

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