function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael MMichael M 

Help with test class for schedulable

Hello, I have the below apex class that is to be used to schedule merging duplicates. How would I write a test class for this? thank you!


global class purgeClDupsSchedulable implements Schedulable{
global void execute(SchedulableContext SC) {
 //** Tweak your account selection ... aka where clause ..
List<AggregateResult> MyMergeList = [SELECT Count(ID) , first_name__c, last_name__c, referral_account__c
FROM Community_Lead__c 
WHERE from_lost_referral__c=TRUE
GROUP BY first_name__c, last_name__c, referral_account__c
HAVING Count(ID) > 1 
ORDER BY Count(ID) desc
LIMIT 2000];


For (AggregateResult Agm : MyMergeList){

   String firstName = string.valueOf(agm.get('first_name__c'));
    String lastName = string.valueOf(agm.get('last_name__c'));
    date dob = date.valueOf(agm.get('dob__c'));
   community_lead__c ToAccount = [Select id, first_name__c, last_name__c from community_lead__c 
                                  where first_name__c like :firstName and last_name__c like :lastName and dob__c = :dob and createddate= today limit 1];
   community_lead__c FromAccount = [Select id, first_name__c, last_name__c from community_lead__c 
                                    where Id != :ToAccount.Id and first_name__c like :firstName and last_name__c like :lastName and dob__c = :dob and createddate= today Limit 1];

   database.merge(ToAccount , FromAccount );
}
}
}

 
Best Answer chosen by Michael M
AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

You can use the following code to get started. I used a sample cron expression but you can use cron maker to build cron expressions for your test class
 
@isTest
private class purgeCIDupsSchedulableTest
{

    static testmethod void schedulerTest() 
    {
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
        insert acc;

       //insert Community_Lead__c record here


        
        Test.startTest();

            String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, new purgeCIDupsSchedulable());
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
            System.assertEquals(CRON_EXP, ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);

        Test.stopTest();
        // Add assert here to validate result
    }
}

If it helps, please mark this as solved

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

You can use the following code to get started. I used a sample cron expression but you can use cron maker to build cron expressions for your test class
 
@isTest
private class purgeCIDupsSchedulableTest
{

    static testmethod void schedulerTest() 
    {
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
        insert acc;

       //insert Community_Lead__c record here


        
        Test.startTest();

            String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, new purgeCIDupsSchedulable());
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
            System.assertEquals(CRON_EXP, ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);

        Test.stopTest();
        // Add assert here to validate result
    }
}

If it helps, please mark this as solved

Anudeep
This was selected as the best answer
Michael MMichael M
Awesome- thank you very much!