+ Start a Discussion
pcmca_2007@yahoo.co.inpcmca_2007@yahoo.co.in 

Writing test class for Schedular

Hi All,

          I am newer to SalesForce.I have to write test class for the following test class.Its urgent please guide me.

 

 

CLASS:

 

global class Reminder_for_DeligatedApprover implements Schedulable
{
    global void execute(SchedulableContext ctx)
    {
        try
        {
            Map<id,string> Map_User_manager = new Map<id,string>();
            List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>(); 
            for(User Obj_Manager : [select id, name, email from user where profile.name in('District Client Manager','MSA GCG Client Manager','National Client Manager','Regional Client Manager') and isactive = false])
            {
                Map_User_manager.put(Obj_Manager.id,Obj_Manager.email);
            }
            datetime dt = system.now();
            dt = dt.addDays(-9);
            for(user obj_user : [select id, name, email,DelegatedApproverId,CreatedDate from user where (DelegatedApproverId =: '' or DelegatedApproverId =: null) and isactive = true and createddate <= :dt])
            {
                if(Map_User_manager.containsKey(obj_user.id))
                {
                    String[] toAddresses = new String[] {Obj_User.email};                  
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setToAddresses(toAddresses);
                    mail.setSubject('Please set Delegated Approver');
                    mail.setPlainTextBody('Please set Delegated Approver.');                  
                    mailList.add(mail);
                }
            }
            if(mailList.size() > 0)
            {
                Messaging.SendEmailResult[] result = Messaging.sendEmail(mailList);
            }
        }
        catch(Exception e)
        {
            system.debug('Exception occured in Reminder_for_DeligatedApprover(): '+ e);
            throw e;
        }
    }
}

 

With Regards

Prashant

piyush parmarpiyush parmar

static testMethod void myUnitTest() {

         Test.startTest();
        String testCronExp = '0 0 1-23 * * ?'; 

 

        Reminder_for_DeligatedApprover rfd = new Reminder_for_DeligatedApprover();

       String productId = System.schedule('Reminder_for_DeligatedApprover', testCronExp, rfd);
        CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, NextFireTime  FROM CronTrigger WHERE id = :productId];
        System.assertEquals(testCronExp, ct.CronExpression);
       
        Test.stopTest();

}

 

 

Cheers

Piyush Parmar