• Phillip Mcelu
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I tried to upload a package including this Apex Class, a message shows up "Upload Failed No test methods found in the Apex code included in the package. At least 75% test coverage is required." Can anyone please help me how to write the test method?
 
Public Class EmailHandler{


Public String newappId;
public static Boolean testmode { get; set; }
static {
            testmode = false;
}

public pagereference sendemail() {
        
        newappId = Apexpages.currentPage().getParameters().get('id');
        List<Tax_Receipt__c> lst_emailfund;
        lst_emailfund = [SELECT id,AccountEmail__c,name,Contact_First_Name__c,CreatedById, Contact_Email__c,Account__c,Account__r.name,Contact__c FROM Tax_Receipt__c WHERE id=:Apexpages.currentPage().getParameters().get('id')  Limit 1];
        
        sendALLemail(lst_emailfund);
        PageReference pageRef = new PageReference('/'+newappId);
        pageRef.setRedirect(true);
        return pageRef;       

}


public static void sendALLemail(List<Tax_Receipt__c> lst_TaxRcpt) {
        
        system.debug('***EMAIL2***');
        Boolean SendEmail = True;
        Task addEmail;
        
        Date todaydate = system.today();
        
   for(Tax_Receipt__c emailfund:lst_TaxRcpt ){     
        
        addEmail = new Task();
        SendEmail=true; 
        if(emailfund.Contact_Email__c == Null && emailfund.AccountEmail__c==Null){           
            SendEmail=false;           
            addEmail.subject = 'Missing Email Address';
            addEmail.Description = 'Tx certificate was not sent because there is no email address No: '+ emailfund.name;        
        }
        if(SendEmail==false){
        
        }
        else{
            
            addEmail.OwnerId = emailfund.createdbyId;
            addEmail.subject = 'Tax Email';
            addEmail.status = 'closed';
            addEmail.Priority = 'Normal';
            addEmail.ActivityDate = System.Today();
            addEmail.Description = 'Emailed tax certificate with attachment PSFATaxCertificate.pdf see Tx certificate No: '+ emailfund.name;
            
             
            String addresses,donor,subject,body;
            
            EmailTemplate templateId; 
            
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            
            
            if(emailfund.Contact_Email__c != null){
                
                addresses = emailfund.Contact_Email__c;                                     
                donor = emailfund.Contact_First_Name__c;
                templateId = [select id, name from EmailTemplate where name =: 'TaxCertificateContact'];    
                        
                addEmail.WhoId = emailfund.Contact__c;
                addEmail.WhatId = emailfund.id;
                
                
                email.setTemplateId(templateId.id);
                email.setTargetObjectId(emailfund.Contact__c);
                
            }
            else{
                
                donor = emailfund.Account__r.name;
                addresses = emailfund.AccountEmail__c;
                addEmail.WhatId = emailfund.Account__c;
                
                 Batch_Email__c BatchEmail = new Batch_Email__c();
                 BatchEmail = [SELECT Date__c,Image_Url__c,Email_Subject__c,Email_Body__c FROM Batch_Email__c limit 1];
                
                subject = 'PSFA TaxCertificate';
                body = '<html><body>Dear'+ ' ' + donor +' '+
                
                
                '<p>'+BatchEmail.Email_Body__c+
               // Attached please find a copy of your Section 18A Tax Certificate for donation/s made to the Peninsula School Feeding Association during the 2014/15 financial year.</p><p>Your generous financial support has ensured that the hungry children on our feeding programme continue to receive healthy cooked meals while at school.</p><br><br>Kind regards<br><br>CHARLES GREY<br><br>FUNDRAISING MANAGER<br>Cell: 0824548808<br>
                '<br><img width = "160" height = "120" src="'+ BatchEmail.Image_Url__c + '"/><br><table><tr><td><a href = "http://www.facebook.com/PSFA.org" >Facebook </a></td><td><a href = "https://twitter.com/SchoolFeeding" >Twitter </a></td></table></body></html>';
        system.debug('****'+body);
        //        https://c.cs17.content.force.com/servlet/servlet.ImageServer?id=015g0000000verY&oid=00Dg0000006SaLh&lastMod=1465468508000" />
                
                 email.setHtmlBody(body );
                //email.setPlainTextBody( body );
                email.setSubject(BatchEmail.Email_Subject__c );
                      
            }  
            
            
        
             
        
        //Attachment get the data
        
          PageReference pdf;
          //  system.debug('***InvestmentType***'+InvestmentType);
          if(emailfund != null){                  
                pdf =  Page.TaxIndEmail;
          }
                   
            pdf.getParameters().put('id',(String)emailfund.id); 
            pdf.setRedirect(true); 
        
            // Take the PDF content
            Blob b; 
           if (testmode)
           {
                b = Blob.valueOf(body);

           }
           else
           {
                 
                b = pdf.getContent();
           }

        // Create the email attachment and the email
            
            
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('PSFATaxCertificate.pdf');
           efa.setBody(b);        
            
            
            system.debug('**addresses**'+addresses);
            String[] toAddresses = addresses.split(':', 0);
            OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'chantal@psfa.org.za'];  
            // Sets the paramaters of the email
            
            if ( owea.size() > 0 ) {
                email.setOrgWideEmailAddressId(owea.get(0).Id);
            }
             
            
            
            email.setSaveAsActivity(true);
            email.setReplyTo('chantal@psfa.org.za');
            email.setToAddresses( toAddresses);
           

            email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

            // Sends the email    
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
            
        
            emailfund.Isclosed__c = true;
          //  update emailfund;
           Insert addEmail;
      }      
   }     
  update lst_TaxRcpt;     
        
        
}    




}
I tried to upload a package including this Apex Class, a message shows up "Upload Failed No test methods found in the Apex code included in the package. At least 75% test coverage is required." Can anyone please help me how to write the test method?
 
Public Class EmailHandler{


Public String newappId;
public static Boolean testmode { get; set; }
static {
            testmode = false;
}

public pagereference sendemail() {
        
        newappId = Apexpages.currentPage().getParameters().get('id');
        List<Tax_Receipt__c> lst_emailfund;
        lst_emailfund = [SELECT id,AccountEmail__c,name,Contact_First_Name__c,CreatedById, Contact_Email__c,Account__c,Account__r.name,Contact__c FROM Tax_Receipt__c WHERE id=:Apexpages.currentPage().getParameters().get('id')  Limit 1];
        
        sendALLemail(lst_emailfund);
        PageReference pageRef = new PageReference('/'+newappId);
        pageRef.setRedirect(true);
        return pageRef;       

}


public static void sendALLemail(List<Tax_Receipt__c> lst_TaxRcpt) {
        
        system.debug('***EMAIL2***');
        Boolean SendEmail = True;
        Task addEmail;
        
        Date todaydate = system.today();
        
   for(Tax_Receipt__c emailfund:lst_TaxRcpt ){     
        
        addEmail = new Task();
        SendEmail=true; 
        if(emailfund.Contact_Email__c == Null && emailfund.AccountEmail__c==Null){           
            SendEmail=false;           
            addEmail.subject = 'Missing Email Address';
            addEmail.Description = 'Tx certificate was not sent because there is no email address No: '+ emailfund.name;        
        }
        if(SendEmail==false){
        
        }
        else{
            
            addEmail.OwnerId = emailfund.createdbyId;
            addEmail.subject = 'Tax Email';
            addEmail.status = 'closed';
            addEmail.Priority = 'Normal';
            addEmail.ActivityDate = System.Today();
            addEmail.Description = 'Emailed tax certificate with attachment PSFATaxCertificate.pdf see Tx certificate No: '+ emailfund.name;
            
             
            String addresses,donor,subject,body;
            
            EmailTemplate templateId; 
            
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            
            
            if(emailfund.Contact_Email__c != null){
                
                addresses = emailfund.Contact_Email__c;                                     
                donor = emailfund.Contact_First_Name__c;
                templateId = [select id, name from EmailTemplate where name =: 'TaxCertificateContact'];    
                        
                addEmail.WhoId = emailfund.Contact__c;
                addEmail.WhatId = emailfund.id;
                
                
                email.setTemplateId(templateId.id);
                email.setTargetObjectId(emailfund.Contact__c);
                
            }
            else{
                
                donor = emailfund.Account__r.name;
                addresses = emailfund.AccountEmail__c;
                addEmail.WhatId = emailfund.Account__c;
                
                 Batch_Email__c BatchEmail = new Batch_Email__c();
                 BatchEmail = [SELECT Date__c,Image_Url__c,Email_Subject__c,Email_Body__c FROM Batch_Email__c limit 1];
                
                subject = 'PSFA TaxCertificate';
                body = '<html><body>Dear'+ ' ' + donor +' '+
                
                
                '<p>'+BatchEmail.Email_Body__c+
               // Attached please find a copy of your Section 18A Tax Certificate for donation/s made to the Peninsula School Feeding Association during the 2014/15 financial year.</p><p>Your generous financial support has ensured that the hungry children on our feeding programme continue to receive healthy cooked meals while at school.</p><br><br>Kind regards<br><br>CHARLES GREY<br><br>FUNDRAISING MANAGER<br>Cell: 0824548808<br>
                '<br><img width = "160" height = "120" src="'+ BatchEmail.Image_Url__c + '"/><br><table><tr><td><a href = "http://www.facebook.com/PSFA.org" >Facebook </a></td><td><a href = "https://twitter.com/SchoolFeeding" >Twitter </a></td></table></body></html>';
        system.debug('****'+body);
        //        https://c.cs17.content.force.com/servlet/servlet.ImageServer?id=015g0000000verY&oid=00Dg0000006SaLh&lastMod=1465468508000" />
                
                 email.setHtmlBody(body );
                //email.setPlainTextBody( body );
                email.setSubject(BatchEmail.Email_Subject__c );
                      
            }  
            
            
        
             
        
        //Attachment get the data
        
          PageReference pdf;
          //  system.debug('***InvestmentType***'+InvestmentType);
          if(emailfund != null){                  
                pdf =  Page.TaxIndEmail;
          }
                   
            pdf.getParameters().put('id',(String)emailfund.id); 
            pdf.setRedirect(true); 
        
            // Take the PDF content
            Blob b; 
           if (testmode)
           {
                b = Blob.valueOf(body);

           }
           else
           {
                 
                b = pdf.getContent();
           }

        // Create the email attachment and the email
            
            
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('PSFATaxCertificate.pdf');
           efa.setBody(b);        
            
            
            system.debug('**addresses**'+addresses);
            String[] toAddresses = addresses.split(':', 0);
            OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'chantal@psfa.org.za'];  
            // Sets the paramaters of the email
            
            if ( owea.size() > 0 ) {
                email.setOrgWideEmailAddressId(owea.get(0).Id);
            }
             
            
            
            email.setSaveAsActivity(true);
            email.setReplyTo('chantal@psfa.org.za');
            email.setToAddresses( toAddresses);
           

            email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

            // Sends the email    
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
            
        
            emailfund.Isclosed__c = true;
          //  update emailfund;
           Insert addEmail;
      }      
   }     
  update lst_TaxRcpt;     
        
        
}    




}