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
Clara GauntClara Gaunt 

Help with test class for inbound email handler with csv attachment which creates records

Hi - Trying to write a successful test class for a class which reads a csv attachment as part of an email service and then creates record. I cannot seem to get my head around how to test the csv string part. Current code is below:
Class:
global class agentsummary implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope envelope){        
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); 
          Messaging.InboundEmail.TextAttachment[] tAttachments = email.textAttachments;
        Messaging.InboundEmail.BinaryAttachment[] bAttachments = email.BinaryAttachments; 
        String csvbody='';
        String[] csvFileLines = new List<String>();
        List<VCC_Reporting_Object__c> vcclist2= New List<VCC_Reporting_Object__c>();        
        system.debug('bAttachments'+ bAttachments);
        if(bAttachments !=null){
            for(Messaging.InboundEmail.BinaryAttachment btt :bAttachments){
                if(btt.filename.endsWith('.csv')){
                    csvbody = btt.body.toString();  
                    system.debug('csvbody'+ csvbody);                  
                                      
                    csvFileLines = csvbody.split('\n');
                    system.debug('csvFileLines'+ csvFileLines);                    
                    for(Integer i=1; i < csvFileLines.size(); i++){                        
                        String[] csvRecordData = csvFileLines[i].split(',');
                        VCC_Reporting_Object__c vccObj2 = new VCC_Reporting_Object__c() ;
                        system.debug('vccObj2'+ vccObj2);   
                        vccObj2.Agent_Id__c = csvRecordData[0] ;
                         vccObj2.Agent_Name1__c = csvRecordData[1];
                        vccObj2.Name = csvRecordData[1];
                        vccObj2.Inbound_Contacts__c = csvRecordData[3];  
                        vccObj2.Outbound_contacts__c = csvRecordData[4];  
                          vccObj2.Unavailable_Time__c = csvRecordData[9]; 
                        vccObj2.Available_Time__c = csvRecordData[10]; 
                        vccObj2.Refused__c = csvRecordData[12]; 
                      
                       // vccObj.body = Blob.valueOf(tAttachment.Body);                 
                        vcclist2.add(vccObj2);                        
                    }     
                }
            }           
            if(vcclist2.size()>0)                
                insert vcclist2;
            system.debug('vcclist2'+ vcclist2);
        }        
        result.success = true;
        return result;        
                                      }     

}

Test class (only covering the email handler part):
@isTest
public class testagentsummary  {
     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 = 'someaddress@email.com';
      email.plainTextBody = 'email body\n2225256325\nTitle';
      
      // 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();
      attachmenttext.body = 'my attachment text';
      attachmenttext.fileName = 'textfiletwo3.txt';
      attachmenttext.mimeTypeSubType = 'texttwo/plain';
      email.textAttachments =   new Messaging.inboundEmail.TextAttachment[] { attachmenttext };
      
      
      // call the email service class and test it with the data in the testMethod
      agentsummary  agent=new agentsummary ();
      agent.handleInboundEmail(email, env);
    
     
   
   }}
  

Any help would be really appreciated
 
SwethaSwetha (Salesforce Developers) 
HI Clara,
You can take reference of these posts to get started:
https://salesforce.stackexchange.com/questions/363124/how-to-create-a-test-class-for-apex-that-reads-csv-file

https://salesforce.stackexchange.com/questions/327121/can-someone-help-me-writing-test-class-as-there-is-email-attachment-iam-really-s

https://salesforce.stackexchange.com/questions/342688/messaging-inboundemailhandler-email-test-class-not-covering-whole-class

Thanks