Skip to main content
Formatted apex code
Source Link
RubenDG
  • 9.6k
  • 2
  • 15
  • 24

global class CertificationEmail implements Database.Batchable{ global String soql=''; global Database.QueryLocator start(Database.BatchableContext bc) {

    Date d = Date.today();
    Date expiryOne = System.today()+30;
    Date expiryTwo = System.today()+7;
    Date expiryThree = System.today()+3;
    String soql = 'SELECT Id, CreatedById,Manager_Email_id__c,Certifications_Email__c,Certification_Expired_Date__c,emp_first_name1__c,Emp_last_name2__c,certification_Names_del__c, Employee__c,Expiry_Date__c FROM Certification__c where Expiry_Date__c=:expiryOne OR Expiry_Date__c=:expiryTwo OR Expiry_Date__c=:expiryThree OR Expiry_Date__c=:d';
    
    return Database.getQueryLocator(soql);
}

global void execute(Database.BatchableContext bc, List<Certification__c> recs) {
    system.debug(recs.size());
    if(!test.isrunningtest()){   
    List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
    for(Certification__c m : recs) {
        List<String> toAddresses = new List<String>();
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        toAddresses.add(m.Certifications_Email__c);
         mail.setToAddresses(toAddresses);
         list<string> ccaddress = new list<string>();
        ccaddress.add(m.Manager_Email_id__c);
        mail.setCcAddresses(ccaddress);
        mail.setSubject('Certificate will expired');
        string body='Dear ';
        body=body+' '+m.emp_first_name1__c+' '+m.Emp_last_name2__c+' ,';
        body=body+'<br/>Your'+' ';           
        body=body+''+m.certification_Names_del__c+ 'Certificate will be Expired on...'+' '+m.Certification_Expired_Date__c+' ,';
        body=body+'Kindly Maintain your Certification.';
        body=body+'<br/>Thanks&Regards,';
        body=body+'<br/>Saleforce COE Team';
        mail.setHtmlBody(body);
        mailList.add(mail);         
    }
    
    Messaging.sendEmail(mailList);
    
}
}
global void finish(Database.BatchableContext bc) {
    
}

}

global class CertificationEmail implements Database.Batchable<sObject>{
    global String soql='';

    global Database.QueryLocator start(Database.BatchableContext bc) {
       
        Date d = Date.today();
        Date expiryOne = System.today()+30;
        Date expiryTwo = System.today()+7;
        Date expiryThree = System.today()+3;
        String soql = 'SELECT Id, CreatedById,Manager_Email_id__c,Certifications_Email__c,Certification_Expired_Date__c,emp_first_name1__c,Emp_last_name2__c,certification_Names_del__c, Employee__c,Expiry_Date__c FROM Certification__c where Expiry_Date__c=:expiryOne OR Expiry_Date__c=:expiryTwo OR Expiry_Date__c=:expiryThree OR Expiry_Date__c=:d';
        
        return Database.getQueryLocator(soql);
    }
    
    global void execute(Database.BatchableContext bc, List<Certification__c> recs) {
        system.debug(recs.size());
        if(!test.isrunningtest()){   
            List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
            for(Certification__c m : recs) {
                List<String> toAddresses = new List<String>();
               
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                toAddresses.add(m.Certifications_Email__c);
                 mail.setToAddresses(toAddresses);
                 list<string> ccaddress = new list<string>();
                ccaddress.add(m.Manager_Email_id__c);
                mail.setCcAddresses(ccaddress);
                mail.setSubject('Certificate will expired');
                string body='Dear ';
                body=body+' '+m.emp_first_name1__c+' '+m.Emp_last_name2__c+' ,';
                body=body+'<br/>Your'+' ';           
                body=body+''+m.certification_Names_del__c+ 'Certificate will be Expired on...'+' '+m.Certification_Expired_Date__c+' ,';
                body=body+'Kindly Maintain your Certification.';
                body=body+'<br/>Thanks&Regards,';
                body=body+'<br/>Saleforce COE Team';
                mail.setHtmlBody(body);
                mailList.add(mail);         
            }

            Messaging.sendEmail(mailList);
        }
    }

    global void finish(Database.BatchableContext bc) {
        
    }
}

Test Class its showing 29% only

@isTest(SeeAllData = true) public class CertificationEmailTestClass { @isTest public static void CertificationEmailNotification(){

    list<Employees__c> ss=new list<Employees__c>();
    Employees__c em=new Employees__c();
    em.name='test89';
    em.Wissen_Employee_ID__c='1456';
    em.Last_Name__c='dumay288';
    em.Working_Email__c='[email protected]';
    em.Date_Of_Joining__c= Date.newInstance(1988, 2, 17);
    em.Laptop_W_P_CLIENT__c='Wissen';
    em.Country__c='India';
    ss.add(em);
    
    insert ss; 
 Employees__c Emp = [Select Last_Name__c,Name From Employees__c where name = 'test89'];
    
         
   // TestFactory.certification();
   list<Certification__c> css=new list<Certification__c>();
    
    Certification__c cer=new Certification__c(Employee__c = em.id);
    cer.Employee__c = emp.id;
    cer.Wissen_COE__c='salesforce coe';
    cer.certification_Names_del__c='Admin 201';
    cer.Certifications_Email__c='[email protected]';
    cer.Manager_Email_id__c = '[email protected]';
    cer.Certifications_Date__c = system.today().adddays(-30);   
    
    css.add(cer);
    insert css;
    
    
    Test.startTest();
    CertificationEmail Email = new CertificationEmail();
    DataBase.executeBatch(Email);
    
    
    Test.stopTest();
   
}
@isTest
public static void CertificationEmailNotification2(){
    
    Test.startTest();
    scheduleExpireNotify sh1 = new scheduleExpireNotify();
    String sch = '0 0 2 * * ?'; 
    system.schedule('Test Territory Check', sch, sh1); 
    Test.stopTest();
    
    
    
}

}

@isTest(SeeAllData = true)
public class CertificationEmailTestClass 
{
    @isTest
    public static  void CertificationEmailNotification(){
        
              
        list<Employees__c> ss=new list<Employees__c>();
        Employees__c em=new Employees__c();
        em.name='test89';
        em.Wissen_Employee_ID__c='1456';
        em.Last_Name__c='dumay288';
        em.Working_Email__c='[email protected]';
        em.Date_Of_Joining__c= Date.newInstance(1988, 2, 17);
        em.Laptop_W_P_CLIENT__c='Wissen';
        em.Country__c='India';
        ss.add(em);
        
        insert ss; 
        Employees__c Emp = [Select Last_Name__c,Name From Employees__c where name = 'test89'];
        
             
       // TestFactory.certification();
       list<Certification__c> css=new list<Certification__c>();
        
        Certification__c cer=new Certification__c(Employee__c = em.id);
        cer.Employee__c = emp.id;
        cer.Wissen_COE__c='salesforce coe';
        cer.certification_Names_del__c='Admin 201';
        cer.Certifications_Email__c='[email protected]';
        cer.Manager_Email_id__c = '[email protected]';
        cer.Certifications_Date__c = system.today().adddays(-30);   
        
        css.add(cer);
        insert css;
        
        
        Test.startTest();
        CertificationEmail Email = new CertificationEmail();
        DataBase.executeBatch(Email);
        
        
        Test.stopTest();
       
    }

    @isTest
    public static void CertificationEmailNotification2(){
        
        Test.startTest();
        scheduleExpireNotify sh1 = new scheduleExpireNotify();
        String sch = '0 0 2 * * ?'; 
        system.schedule('Test Territory Check', sch, sh1); 
        Test.stopTest();

    }
}

global class CertificationEmail implements Database.Batchable{ global String soql=''; global Database.QueryLocator start(Database.BatchableContext bc) {

    Date d = Date.today();
    Date expiryOne = System.today()+30;
    Date expiryTwo = System.today()+7;
    Date expiryThree = System.today()+3;
    String soql = 'SELECT Id, CreatedById,Manager_Email_id__c,Certifications_Email__c,Certification_Expired_Date__c,emp_first_name1__c,Emp_last_name2__c,certification_Names_del__c, Employee__c,Expiry_Date__c FROM Certification__c where Expiry_Date__c=:expiryOne OR Expiry_Date__c=:expiryTwo OR Expiry_Date__c=:expiryThree OR Expiry_Date__c=:d';
    
    return Database.getQueryLocator(soql);
}

global void execute(Database.BatchableContext bc, List<Certification__c> recs) {
    system.debug(recs.size());
    if(!test.isrunningtest()){   
    List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
    for(Certification__c m : recs) {
        List<String> toAddresses = new List<String>();
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        toAddresses.add(m.Certifications_Email__c);
         mail.setToAddresses(toAddresses);
         list<string> ccaddress = new list<string>();
        ccaddress.add(m.Manager_Email_id__c);
        mail.setCcAddresses(ccaddress);
        mail.setSubject('Certificate will expired');
        string body='Dear ';
        body=body+' '+m.emp_first_name1__c+' '+m.Emp_last_name2__c+' ,';
        body=body+'<br/>Your'+' ';           
        body=body+''+m.certification_Names_del__c+ 'Certificate will be Expired on...'+' '+m.Certification_Expired_Date__c+' ,';
        body=body+'Kindly Maintain your Certification.';
        body=body+'<br/>Thanks&Regards,';
        body=body+'<br/>Saleforce COE Team';
        mail.setHtmlBody(body);
        mailList.add(mail);         
    }
    
    Messaging.sendEmail(mailList);
    
}
}
global void finish(Database.BatchableContext bc) {
    
}

}

Test Class its showing 29% only

@isTest(SeeAllData = true) public class CertificationEmailTestClass { @isTest public static void CertificationEmailNotification(){

    list<Employees__c> ss=new list<Employees__c>();
    Employees__c em=new Employees__c();
    em.name='test89';
    em.Wissen_Employee_ID__c='1456';
    em.Last_Name__c='dumay288';
    em.Working_Email__c='[email protected]';
    em.Date_Of_Joining__c= Date.newInstance(1988, 2, 17);
    em.Laptop_W_P_CLIENT__c='Wissen';
    em.Country__c='India';
    ss.add(em);
    
    insert ss; 
 Employees__c Emp = [Select Last_Name__c,Name From Employees__c where name = 'test89'];
    
         
   // TestFactory.certification();
   list<Certification__c> css=new list<Certification__c>();
    
    Certification__c cer=new Certification__c(Employee__c = em.id);
    cer.Employee__c = emp.id;
    cer.Wissen_COE__c='salesforce coe';
    cer.certification_Names_del__c='Admin 201';
    cer.Certifications_Email__c='[email protected]';
    cer.Manager_Email_id__c = '[email protected]';
    cer.Certifications_Date__c = system.today().adddays(-30);   
    
    css.add(cer);
    insert css;
    
    
    Test.startTest();
    CertificationEmail Email = new CertificationEmail();
    DataBase.executeBatch(Email);
    
    
    Test.stopTest();
   
}
@isTest
public static void CertificationEmailNotification2(){
    
    Test.startTest();
    scheduleExpireNotify sh1 = new scheduleExpireNotify();
    String sch = '0 0 2 * * ?'; 
    system.schedule('Test Territory Check', sch, sh1); 
    Test.stopTest();
    
    
    
}

}

global class CertificationEmail implements Database.Batchable<sObject>{
    global String soql='';

    global Database.QueryLocator start(Database.BatchableContext bc) {
       
        Date d = Date.today();
        Date expiryOne = System.today()+30;
        Date expiryTwo = System.today()+7;
        Date expiryThree = System.today()+3;
        String soql = 'SELECT Id, CreatedById,Manager_Email_id__c,Certifications_Email__c,Certification_Expired_Date__c,emp_first_name1__c,Emp_last_name2__c,certification_Names_del__c, Employee__c,Expiry_Date__c FROM Certification__c where Expiry_Date__c=:expiryOne OR Expiry_Date__c=:expiryTwo OR Expiry_Date__c=:expiryThree OR Expiry_Date__c=:d';
        
        return Database.getQueryLocator(soql);
    }
    
    global void execute(Database.BatchableContext bc, List<Certification__c> recs) {
        system.debug(recs.size());
        if(!test.isrunningtest()){   
            List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
            for(Certification__c m : recs) {
                List<String> toAddresses = new List<String>();
               
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                toAddresses.add(m.Certifications_Email__c);
                 mail.setToAddresses(toAddresses);
                 list<string> ccaddress = new list<string>();
                ccaddress.add(m.Manager_Email_id__c);
                mail.setCcAddresses(ccaddress);
                mail.setSubject('Certificate will expired');
                string body='Dear ';
                body=body+' '+m.emp_first_name1__c+' '+m.Emp_last_name2__c+' ,';
                body=body+'<br/>Your'+' ';           
                body=body+''+m.certification_Names_del__c+ 'Certificate will be Expired on...'+' '+m.Certification_Expired_Date__c+' ,';
                body=body+'Kindly Maintain your Certification.';
                body=body+'<br/>Thanks&Regards,';
                body=body+'<br/>Saleforce COE Team';
                mail.setHtmlBody(body);
                mailList.add(mail);         
            }

            Messaging.sendEmail(mailList);
        }
    }

    global void finish(Database.BatchableContext bc) {
        
    }
}

Test Class its showing 29% only

@isTest(SeeAllData = true)
public class CertificationEmailTestClass 
{
    @isTest
    public static  void CertificationEmailNotification(){
        
              
        list<Employees__c> ss=new list<Employees__c>();
        Employees__c em=new Employees__c();
        em.name='test89';
        em.Wissen_Employee_ID__c='1456';
        em.Last_Name__c='dumay288';
        em.Working_Email__c='[email protected]';
        em.Date_Of_Joining__c= Date.newInstance(1988, 2, 17);
        em.Laptop_W_P_CLIENT__c='Wissen';
        em.Country__c='India';
        ss.add(em);
        
        insert ss; 
        Employees__c Emp = [Select Last_Name__c,Name From Employees__c where name = 'test89'];
        
             
       // TestFactory.certification();
       list<Certification__c> css=new list<Certification__c>();
        
        Certification__c cer=new Certification__c(Employee__c = em.id);
        cer.Employee__c = emp.id;
        cer.Wissen_COE__c='salesforce coe';
        cer.certification_Names_del__c='Admin 201';
        cer.Certifications_Email__c='[email protected]';
        cer.Manager_Email_id__c = '[email protected]';
        cer.Certifications_Date__c = system.today().adddays(-30);   
        
        css.add(cer);
        insert css;
        
        
        Test.startTest();
        CertificationEmail Email = new CertificationEmail();
        DataBase.executeBatch(Email);
        
        
        Test.stopTest();
       
    }

    @isTest
    public static void CertificationEmailNotification2(){
        
        Test.startTest();
        scheduleExpireNotify sh1 = new scheduleExpireNotify();
        String sch = '0 0 2 * * ?'; 
        system.schedule('Test Territory Check', sch, sh1); 
        Test.stopTest();

    }
}
Source Link

Batch Apex Class Send notification Test Showing only 29%

global class CertificationEmail implements Database.Batchable{ global String soql=''; global Database.QueryLocator start(Database.BatchableContext bc) {

    Date d = Date.today();
    Date expiryOne = System.today()+30;
    Date expiryTwo = System.today()+7;
    Date expiryThree = System.today()+3;
    String soql = 'SELECT Id, CreatedById,Manager_Email_id__c,Certifications_Email__c,Certification_Expired_Date__c,emp_first_name1__c,Emp_last_name2__c,certification_Names_del__c, Employee__c,Expiry_Date__c FROM Certification__c where Expiry_Date__c=:expiryOne OR Expiry_Date__c=:expiryTwo OR Expiry_Date__c=:expiryThree OR Expiry_Date__c=:d';
    
    return Database.getQueryLocator(soql);
}

global void execute(Database.BatchableContext bc, List<Certification__c> recs) {
    system.debug(recs.size());
    if(!test.isrunningtest()){   
    List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
    for(Certification__c m : recs) {
        List<String> toAddresses = new List<String>();
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        toAddresses.add(m.Certifications_Email__c);
         mail.setToAddresses(toAddresses);
         list<string> ccaddress = new list<string>();
        ccaddress.add(m.Manager_Email_id__c);
        mail.setCcAddresses(ccaddress);
        mail.setSubject('Certificate will expired');
        string body='Dear ';
        body=body+' '+m.emp_first_name1__c+' '+m.Emp_last_name2__c+' ,';
        body=body+'<br/>Your'+' ';           
        body=body+''+m.certification_Names_del__c+ 'Certificate will be Expired on...'+' '+m.Certification_Expired_Date__c+' ,';
        body=body+'Kindly Maintain your Certification.';
        body=body+'<br/>Thanks&Regards,';
        body=body+'<br/>Saleforce COE Team';
        mail.setHtmlBody(body);
        mailList.add(mail);         
    }
    
    Messaging.sendEmail(mailList);
    
}
}
global void finish(Database.BatchableContext bc) {
    
}

}

Test Class its showing 29% only

@isTest(SeeAllData = true) public class CertificationEmailTestClass { @isTest public static void CertificationEmailNotification(){

    list<Employees__c> ss=new list<Employees__c>();
    Employees__c em=new Employees__c();
    em.name='test89';
    em.Wissen_Employee_ID__c='1456';
    em.Last_Name__c='dumay288';
    em.Working_Email__c='[email protected]';
    em.Date_Of_Joining__c= Date.newInstance(1988, 2, 17);
    em.Laptop_W_P_CLIENT__c='Wissen';
    em.Country__c='India';
    ss.add(em);
    
    insert ss; 
 Employees__c Emp = [Select Last_Name__c,Name From Employees__c where name = 'test89'];
    
         
   // TestFactory.certification();
   list<Certification__c> css=new list<Certification__c>();
    
    Certification__c cer=new Certification__c(Employee__c = em.id);
    cer.Employee__c = emp.id;
    cer.Wissen_COE__c='salesforce coe';
    cer.certification_Names_del__c='Admin 201';
    cer.Certifications_Email__c='[email protected]';
    cer.Manager_Email_id__c = '[email protected]';
    cer.Certifications_Date__c = system.today().adddays(-30);   
    
    css.add(cer);
    insert css;
    
    
    Test.startTest();
    CertificationEmail Email = new CertificationEmail();
    DataBase.executeBatch(Email);
    
    
    Test.stopTest();
   
}
@isTest
public static void CertificationEmailNotification2(){
    
    Test.startTest();
    scheduleExpireNotify sh1 = new scheduleExpireNotify();
    String sch = '0 0 2 * * ?'; 
    system.schedule('Test Territory Check', sch, sh1); 
    Test.stopTest();
    
    
    
}

}