• harsh vikram
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I have created a schedulable class to send birthday emails to contacts. The script uses two custom field Birth Month(text) and Birth Date(text). I am not able to get 75% code coverage for my test class. I am new to salesforce and struggling with this for couple of days now. Hope I can find the solution through this forum. 

My apex class and test class are given below
 
public class ScheduleBirthdayEmails implements Schedulable{
    public void execute(SchedulableContext sc){
        Date todayDate = Date.today();
        String birthDate = String.valueOf(todayDate.day());
        String birthMonth;
        switch on todayDate.month() {
            when 1 {
                birthMonth = 'Jan';
            }
            when 2 {
                birthMonth = 'Feb';
            }
            when 3 {
                birthMonth = 'Mar';
            }
            when 4 {
                birthMonth = 'Apr';
            }
            when 5 {
                birthMonth = 'May';
            }
            when 6 {
                birthMonth = 'Jun';
            }
            when 7 {
                birthMonth = 'Jul';
            }
            when 8 {
                birthMonth = 'Aug';
            }
            when 9 {
                birthMonth = 'Sep';
            }
            when 10 {
                birthMonth = 'Oct';
            }
            when 11 {
                birthMonth = 'Nov';
            }
            when 12 {
                birthMonth = 'Dec';
            }
        }
        String perEmail;
        String workEmail;
        String altEmail;
        String prefEmail;
        List<Id> idList = new List<Id>();
        List<String> emailList = new List<String>();
                
        List<Contact> conList = [select id,npe01__Preferred_Email__c,npe01__HomeEmail__c,npe01__AlternateEmail__c,npe01__WorkEmail__c from Contact where Birthday_Email__c = TRUE AND date__c=:birthDate AND month__c =:birthMonth];
               
        for(Contact c:conList){
            perEmail = c.npe01__HomeEmail__c;
            workEmail = c.npe01__WorkEmail__c;
            altEmail = c.npe01__AlternateEmail__c;
            prefEmail = c.npe01__Preferred_Email__c;
            
            if(prefEmail == 'Personal' && perEmail.length() > 0){
                emailList.add(perEmail);
                idList.add(c.id);
            }
            if(prefEmail == 'Work' && workEmail.length() > 0){
                emailList.add(workEmail);
                idList.add(c.id);
            }
            if(prefEmail == 'Alternate' && altEmail.length() > 0){
                emailList.add(altEmail);
                idList.add(c.id);
            }
        }
  		
         if(idList.size() > 0) {
            EmailTemplate template = [select id from EmailTemplate where developername = 'Send_Birthday_Emails' LIMIT 1];
            Messaging.MassEmailMessage emails = new Messaging.MassEmailMessage();
            emails.setTargetObjectIds(idList);
            emails.setTemplateId(template.id);
            Messaging.sendEmail(new Messaging.MassEmailMessage[]{emails});
        }
    }

}
 
@isTest
public class BirthdayEmailTest {
    @testSetup
    static void setupContactData(){
         List<Contact> conList = new List<Contact>();
        Date todayDate = System.today();
        String birthMonth = 'Apr';
        switch on todayDate.month() {
            when 1 {
                birthMonth = 'Jan';
            }
            when 2 {
                birthMonth = 'Feb';
            }
            when 3 {
                birthMonth = 'Mar';
            }
            when 4 {
                birthMonth = 'Apr';
            }
            when 5 {
                birthMonth = 'May';
            }
            when 6 {
                birthMonth = 'Jun';
            }
            when 7 {
                birthMonth = 'Jul';
            }
            when 8 {
                birthMonth = 'Aug';
            }
            when 9 {
                birthMonth = 'Sep';
            }
            when 10 {
                birthMonth = 'Oct';
            }
            when 11 {
                birthMonth = 'Nov';
            }
            when 12 {
                birthMonth = 'Dec';
            }
        }
        
        Contact c1 = new Contact();
        c1.LastName = 'Contact1';
        c1.Date__c = String.valueOf(todayDate.day());
        c1.Month__c = birthMonth;
        c1.Birthday_Email__c = true;
        c1.npe01__HomeEmail__c = 'harshvikram16@gmail.com';
        c1.npe01__Preferred_Email__c = 'Personal';
        conList.add(c1);
        
        Contact c2 = new Contact();
        c2.LastName = 'Contact2';
        c2.Date__c = String.valueOf(todayDate.day());
        c2.Month__c = birthMonth;
        c2.Birthday_Email__c = true;
        c2.npe01__WorkEmail__c = 'kriti.c30@gmail.com';
        c2.npe01__Preferred_Email__c = 'Work';
        conList.add(c2);
        
        Contact c3 = new Contact();
        c3.LastName = 'Contact3';
        c3.Date__c = String.valueOf(todayDate.day());
        c3.Month__c = birthMonth;
        c3.Birthday_Email__c = true;
        c3.npe01__AlternateEmail__c = 'alternateemail@test.com';
        c3.npe01__Preferred_Email__c = 'Alternate';
        conList.add(c3);
        
        insert conList;
        
        
    }
    
    @istest
    static void testScheduleBirthdayEmails(){
        String CRON_EXP = '0 0 0 * * ? *';
        Test.startTest();
        ScheduleBirthdayEmails sbe = new ScheduleBirthdayEmails();
        String jobId = System.schedule('ScheduleBirthdayEmails', CRON_EXP, sbe);
        ScheduleBirthdayEmails sbe = new ScheduleBirthdayEmails();
        CronTrigger ct = [select id, cronexpression,timestriggered, nextfiretime from crontrigger where id=:jobid];
        System.assertequals(CRON_EXP,ct.CronExpression);
        
        Test.stopTest();
        
    }
}

 
Hello All  - We are planning to integrate PayPal with Salesforce. We basically want to integrate Salesforce NPSP solution with PayPal. This integration will enable us to capture donations made by donors in PayPal mobile/desktop application as Opportunity/Gift in Salesforce. What options do we have here?