• Andrejs Antonovs
  • NEWBIE
  • 20 Points
  • Member since 2015
  • eCommerce Project Manaager
  • Scandiweb


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

How ar eyou doing today?

Maybe you can try to help me with one issue. I have Apex Class - EmployeeMonthlyEmail, which takes collection of employees, and sends them emails. And another Test Apex Class - TestEmployeeMonthlyEmail for code coverage. Unfortunately, code coverage is 0%, and I am unable to understand why.

Please see code below. Based on test run results, situations with Good and Bad emails provided to a function are covered, and tests finish correctly returning True/False as expected. However, Code Coverage is at 0% and 0/27 lines.

Here is a screen shot with code coverage info and test run:
http://screencast.com/t/tWsfyHud

Please note that during debug execution, code works as expected and emails are received on GMail account. It means code is written corretly, but Code Coverage probably is done inappropriately.

Any ideas what is wrong in this situation?

p.s. TestDataFactory class works as expected - generates records, previously test code did not use DataFactory and code coverage was also 0%

------------ Apex Class EmployeeMonthlyEmail
public class EmployeeMonthlyEmail {
    public static Boolean sendEmailsToEmployees() {
        List<Employee__c> empList = 
            [SELECT Name, FirstName__c, LastName__c, Email__c
             FROM Employee__c
             WHERE Status__c != 'Disabled'
            ];
        
        for(Employee__c emp:empList){
            sendEmailToEmployee(emp);
        }
        
        return true;
    }
    
    public static Boolean sendEmailToEmployee(Employee__c emp){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setToAddresses(new String[]{emp.Email__c});
        mail.setReplyTo('name.surname@gmail.com');
        mail.setSenderDisplayName('Salesforce');
        mail.setSubject('Information about ' + emp.FirstName__c + ' ' + emp.LastName__c);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setCharset('UTF-8');
        
        mail.setHtmlBody(
            '<p>Hello ' + emp.Name +',</p>'+
            '<p>Please check information we have, and let us know if we should update it.'+
            '<br />Thank you in advance ;)</p>'+
            '<p>First Name: '+ emp.FirstName__c + '<br />'+
            'Last Name: '+ emp.LastName__c + '</p>');
        
        try {
            List<Messaging.SendEmailResult> results = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            
            if (!results.get(0).isSuccess()) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            System.debug('Exception has occurred: ' + e.getMessage());
            return false;
        }
    }
}

------------ Test Apex Class EmployeeMonthlyEmail
@isTest
public class TestEmployeeMonthlyEmail {
    
    @isTest static void testSendEmailToEmployeeSuccess(){
        List<Employee__c> empList = TestDataFactory.createEmployees();
        Boolean result = EmployeeMonthlyEmail.sendEmailToEmployee(empList[0]);
        System.assertEquals(true,result);
    }
    
    @isTest static void testSendEmailToEmployeeFail(){
        List<Employee__c> empList = TestDataFactory.createEmployees();
        empList[0].Email__c = 'bademailname.com';
        Boolean result = EmployeeMonthlyEmail.sendEmailToEmployee(empList[0]);
        System.assertEquals(false,result);
    }
    
    @isTest static void testSendEmailsToEmployees(){
        List<Employee__c> empList = TestDataFactory.createEmployees();
        Boolean result = EmployeeMonthlyEmail.sendEmailsToEmployees();
        System.assertEquals(true,result);
    }
}
Hello.

How ar eyou doing today?

Maybe you can try to help me with one issue. I have Apex Class - EmployeeMonthlyEmail, which takes collection of employees, and sends them emails. And another Test Apex Class - TestEmployeeMonthlyEmail for code coverage. Unfortunately, code coverage is 0%, and I am unable to understand why.

Please see code below. Based on test run results, situations with Good and Bad emails provided to a function are covered, and tests finish correctly returning True/False as expected. However, Code Coverage is at 0% and 0/27 lines.

Here is a screen shot with code coverage info and test run:
http://screencast.com/t/tWsfyHud

Please note that during debug execution, code works as expected and emails are received on GMail account. It means code is written corretly, but Code Coverage probably is done inappropriately.

Any ideas what is wrong in this situation?

p.s. TestDataFactory class works as expected - generates records, previously test code did not use DataFactory and code coverage was also 0%

------------ Apex Class EmployeeMonthlyEmail
public class EmployeeMonthlyEmail {
    public static Boolean sendEmailsToEmployees() {
        List<Employee__c> empList = 
            [SELECT Name, FirstName__c, LastName__c, Email__c
             FROM Employee__c
             WHERE Status__c != 'Disabled'
            ];
        
        for(Employee__c emp:empList){
            sendEmailToEmployee(emp);
        }
        
        return true;
    }
    
    public static Boolean sendEmailToEmployee(Employee__c emp){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setToAddresses(new String[]{emp.Email__c});
        mail.setReplyTo('name.surname@gmail.com');
        mail.setSenderDisplayName('Salesforce');
        mail.setSubject('Information about ' + emp.FirstName__c + ' ' + emp.LastName__c);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setCharset('UTF-8');
        
        mail.setHtmlBody(
            '<p>Hello ' + emp.Name +',</p>'+
            '<p>Please check information we have, and let us know if we should update it.'+
            '<br />Thank you in advance ;)</p>'+
            '<p>First Name: '+ emp.FirstName__c + '<br />'+
            'Last Name: '+ emp.LastName__c + '</p>');
        
        try {
            List<Messaging.SendEmailResult> results = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            
            if (!results.get(0).isSuccess()) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            System.debug('Exception has occurred: ' + e.getMessage());
            return false;
        }
    }
}

------------ Test Apex Class EmployeeMonthlyEmail
@isTest
public class TestEmployeeMonthlyEmail {
    
    @isTest static void testSendEmailToEmployeeSuccess(){
        List<Employee__c> empList = TestDataFactory.createEmployees();
        Boolean result = EmployeeMonthlyEmail.sendEmailToEmployee(empList[0]);
        System.assertEquals(true,result);
    }
    
    @isTest static void testSendEmailToEmployeeFail(){
        List<Employee__c> empList = TestDataFactory.createEmployees();
        empList[0].Email__c = 'bademailname.com';
        Boolean result = EmployeeMonthlyEmail.sendEmailToEmployee(empList[0]);
        System.assertEquals(false,result);
    }
    
    @isTest static void testSendEmailsToEmployees(){
        List<Employee__c> empList = TestDataFactory.createEmployees();
        Boolean result = EmployeeMonthlyEmail.sendEmailsToEmployees();
        System.assertEquals(true,result);
    }
}
Hi, I'm having some difficulty with the Defining Sharing Rules challenge. The error message I am getting says "Challenge not yet complete...here's what's wrong: An object with the API name Project__c does not exist or does not contain the Priority__c picklist field."
Here's a screenshot of my object with my field. is this an error with the checker system, or am I misunderstanding something? Thanks!
Object screenshot