2

I have an email service that creates a new "training" record. the email body is broken down to populate several fields.

Class:

global class ProcessTraining implements Messaging.InboundEmailHandler {

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

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


   String[] emailBody = email.plainTextBody.split('\n', 0);

Training__c em;

String CertificationAttempts = emailBody[22].substringAfter(': ').trim();
String ScorePercent = emailBody[17].substringAfter(': ').trim();
String TimeSpent = emailBody[11].substringAfter(': ').trim();
String datecomp = emailBody[9].substringAfter(': ');
String TrainingName= emailBody[8].substringAfter(': ').trim();
String ContactEmail = emailBody[7].substringAfter(': ').trim();


String mm=datecomp.substringBefore('/');
String dd=datecomp.substringBetween('/','/');
String yy=datecomp.subStringAfterlast('/').trim();

date myDate=Date.newInstance(integer.valueOf(yy), 
     integer.valueOf(mm), 
     integer.valueOf(dd));

em= new Training__c (Message__c = email.plainTextBody);
em.Date_training_completed__c = myDate;
em.Number_of_certification_attempts__c = integer.valueof(CertificationAttempts);
em.Total_time_in_training_course__c =  decimal.valueof(TimeSpent);
em.Name = TrainingName;
em.Training_certification_score__c = decimal.valueof(ScorePercent);
Contact con = [SELECT id,email FROM Contact WHERE email = :ContactEmail];
em.Contact__c = con.id;

 insert em;


   return result;


} //close class

  }

Test Coverage:

@isTest

public class ProcessTrainingTest 
{ 
   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 = 'Create Contact';
      email.fromAddress = '[email protected]';
      email.plainTextBody = 'email body\n2225256325\nTitle:\ntest:';


      // call the email service class and test it with the data in the testMethod
      ProcessTraining testInbound=new ProcessTraining ();
      testInbound.handleInboundEmail(email, env);



   }
   }

Whenever I run the test I get an error message:

Error Message System.ListException: List index out of bounds: 22 Stack Trace Class.ProcessTraining.handleInboundEmail: line 16, column 1 Class.ProcessTrainingTest.TestinBoundEmail: line 20, column 1

Can you help me out in getting this class covered?

2
  • So it's failing to process the date? Your test email body doesn't appear to have any dates within it.
    – Mike Chale
    Commented Aug 14, 2013 at 12:10
  • I tried to comment out the date section, and got the exact error...
    – Itay B
    Commented Aug 14, 2013 at 14:23

2 Answers 2

1

When the line below executes with the test data 'email body\n2225256325\nTitle:\ntest:' it will only split the text into 4, so when you reference elements in the array such as emailBody[9] its giving you the out of bounds exception

String[] emailBody = email.plainTextBody.split('\n', 0);

Change your test so that it has the same data that would be in the email in a live system and also change your logic to be tolerant of emailBody being different sizes. Something like the code below would achieve this:

for(integer i=0 ; i < emailBody.size(); i++) {
    if (i == 22) {
         CertificationAttempts = emailBody[i].substringAfter(': ').trim();
    } 
}

Also consider moving the substringAfter logic to a method so that you don't repeat yourself. Eg create a method such as:

private String mySubstring(String s) {
    if (s != null) {
        return s.substringAfter(': ').trim()
    }
    return null;
}
0

Try adding some debugging when you parse the email to see what that line really is ( emailBody[??] ).

ex:

String[] emailBody = email.plainTextBody.split('\n', 0);

for(integer i=0;i<emailBody.size();i++)
    system.debug('emailBody['+string.valueof(i)+'] = '+emailBody[i]);

You must log in to answer this question.

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