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
LaurenP6777LaurenP6777 

Email Handler Code Coverage Help

Hi guys, 

I wrote an email handler and test class in my sandbox. The coverage is 100%. However, when I try to deploy the email handler class and test class to production, i get the following error:

Your code coverage is 0%. You need at least 75% coverage to complete this deployment.EmailNSSOs

I don't know how I can fix this or why it is saying this. My overall production coverage is 76%. 
/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * email.
 */
global class EmailNSSOs implements Messaging.InboundEmailHandler {

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

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

        String myPlainText= '';
  
  // Add the email plain text into the local variable 
 
  myPlainText = email.plainTextBody;
 String EmailFrom = 'Sender: '+email.FromAddress;
 String EmailTo = 'To: '+email.toAddresses;
 String Emailcc= 'Cc: '+email.ccAddresses;
 String EmailHeaders = 'Detail: '+email.headers;
 String FullDescription = EmailFrom+'\n'+EmailTo+'\n'+Emailcc+'\n'+myPlainText;
 
  String mysubject = email.subject;
  String Numb = mysubject.substringBetween('NSSO-',' ');
  String NSSONumber = 'NSSO-'+Numb;
  
 

 Task[] newTask = new Task[0];
 
  { 
  

  // Try to lookup any users based on the email from address
 
  
   NSSO_Parent__c vUS = [Select Id, Name
    From NSSO_Parent__c
    Where Name = :NSSONumber 
    Limit 1];
  
  User vUser = [Select Id, Email FROM User WHERE Username=:email.fromAddress LIMIT 1];
  
  
  // Add a new Task to the contact record we just found above.
  newTask.add(new Task(Description =  FullDescription,
       Priority = 'Normal',
       Status = 'Completed',
       Type = 'System Generated Email',
        Subject = email.subject,
        IsReminderSet = true,
        OwnerId=vUser.Id,
        ActivityDate=System.today(),
        ReminderDateTime = System.now()+1,
        WhatId =  vUS.Id));

 // Insert the new Task 
 insert newTask;

  

                if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
        Attachment attachment = new Attachment();
        // attach to the newly created contact record
        attachment.ParentId = vUS.Id;
        attachment.Name = email.binaryAttachments[i].filename;
        attachment.Body = email.binaryAttachments[i].body;
        insert attachment;
    }
            }
     
      
    result.success = true;
        return result;
    }
    
   } 
   }
 
@isTest
private class testEmailAlertEmailNSSOs{

static testMethod void testMe() {

 Profile p = [Select id from Profile where name = 'System Administrator'];


User u = new User(alias = 'mo123', email='mousey.1234@covance.com',
      emailencodingkey='UTF-8', lastname='123', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p.Id, country='United States',
      CVD_Division__c='ED', User_Type__c='AE',
      timezonesidkey='America/Los_Angeles', username='mousey.1234@covance.com');
  insert u;
  
             
        
        NSSO_Parent__c q = new NSSO_Parent__c();
        q.Requestor_s_Name__c='Lauren Hanna';
        q.Requestor_s_E_mail__c='lauren.hanna@covance.com';
        q.Sponsor_s_Name__c='mouse';
        q.Protocol_Name__c='TEST';
        q.Total_Duration_in_months__c=10;
        q.KRD_Date__c=Date.newInstance(2017,01,01);
        q.Ownerid=u.id;
        

    Database.insert(q);
    
    
    
    Test.StartTest();

  // create a new email and envelope object
  Messaging.InboundEmail email = new Messaging.InboundEmail() ;
  Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
  
  
  NSSO_Parent__c insertedQt = [SELECT name FROM NSSO_Parent__c WHERE ID = :q.Id];

  // setup the data for the email
  email.subject = 'test '+insertedQt.name+' Account';
  email.fromname = 'FirstName LastName';
  email.fromAddress = 'mousey.1234@covance.com';

  
   // add an attachment
  Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
  attachment.body = blob.valueOf('my attachment text');
  attachment.fileName = 'textfile.txt';
  attachment.mimeTypeSubType = 'text/plain';

  email.binaryAttachments =
    new Messaging.inboundEmail.BinaryAttachment[] { attachment };

  

  // call the email service class and test it with the data in the testMethod
  EmailNSSOs emailProcess = new EmailNSSOs(); 
  emailProcess.handleInboundEmail(email, env);
  
   
  
  
Test.StopTest();

  // query for the contact the email service created
    
      Task Tsk2 = [select id, Type, status, subject, whatid from Task
    where whatid=:q.id];

 
 
    System.assertEquals(Tsk2.Status,'Completed');
  System.assertEquals(Tsk2.Type,'System Generated Email');
  

 
}
}
Temoc MunozTemoc Munoz
Are you including your trigger in the change set as well?