• Jim McGrath 6
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

hello All,

 

Here I'm again with some un-coverage issue for Apex In-BoundEmailService class in sfdc.

 

Here is myInbound emailservice class-

 

global class inBoundEmail implements Messaging.InboundEmailHandler
{
   global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope)
   {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        String subToCompare = 'Create Contact';

        if(email.subject.equalsIgnoreCase(subToCompare))
        {
            Contact c = new Contact();
            c.Email=email.fromAddress;
            
            // capture phone number and city also from incoming email.
            // Splits each line by the terminating newline character  
            // and looks for the position of the phone number and city 
            String[] emailBody = email.plainTextBody.split('\n', 0);
            c.LastName=emailBody[0].substring(0);
            c.Phone = emailBody[1].substring(0);
            c.Title = emailBody[2].substring(0);
                       
            insert c;
            
            // Save attachments, if any
            if (email.textAttachments != null)
            {
            for(Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments)
            {
            Attachment attachment = new Attachment();

            attachment.Name = tAttachment.fileName;
            attachment.Body = Blob.valueOf(tAttachment.body);
            attachment.ParentId = c.Id;
            insert attachment;
            }
            
            }

            //Save any Binary Attachment
            
            if (email.binaryAttachments != null)
            {
            for(Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
            Attachment attachment = new Attachment();

            attachment.Name = bAttachment.fileName;
            attachment.Body = bAttachment.body;
            attachment.ParentId = c.Id;
            insert attachment;
            }
           } 
        }

        result.success = true;
        return result;
             
   }
   }

Thisis the Test Class which is creating no error but it's not covering all test conditions.

 

//Test Method for main class
   
   static testMethod void TestinBoundEmail()
   {
     // 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 = 'Test Job Applicant';
  email.fromAddress = 'someaddress@email.com';
  
  // add an Binary attachment
  
  Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
  attachment.body = blob.valueOf('my attachment text');
  attachment.fileName = 'textfileone.txt';
  attachment.mimeTypeSubType = 'text/plain';
  email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };
 
  // add an Text atatchment
  
  Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
  attachment.body = blob.valueOf('my attachment text');
  attachment.fileName = 'textfiletwo.txt';
  attachment.mimeTypeSubType = 'texttwo/plain';
  email.textAttachments =   new Messaging.inboundEmail.TextAttachment[] { attachmenttext };
 
  // call the email service class and test it with the data in the testMethod
  inBoundEmail  testInbound=new inBoundEmail ();
  testInbound.handleInboundEmail(email, env);
    
   // create a contact data
   
    Contact testContact= new Contact();
    testContact.Email='someaddress@email.com';
    testContact.LastName='lastname';
    testContact.Phone='1234567234';
    testContact.Title='hello';
    insert testContact;
    
    system.debug('insertedcontact id===>' +testContact.Id);
   
   // Create Attachmenat data
   
  Attachment attachmnt =new Attachment();
  attachmnt.name='textfileone.txt';
  attachmnt.body =blob.valueOf('my attachment text');
  attachmnt.ParentId =testContact.Id;
  insert  attachmnt ;
   
   
   }
   

 

Could any one please make me correct in this case.

 

I tried to put all conditions and test data still it's creating problem.

 

Thanks for your valuable suggestions.

 

Thanks & regards,

jaanVivek