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
Steven Wellman 28Steven Wellman 28 

Help - scheduled apex test class

I've written this scheduable apex class but I can't figure out how to write a test class. 
global class UpdateCSMTeam implements Schedulable {
    
    global void execute(SchedulableContext ctx) {

    // Create a list of the accounts to update
    List<Account> myList = new List<Account>();

    // Create a list of all the customer accounts
    List<Account> accountList = [SELECT Id, Account_Manager__c FROM Account WHERE Type = 'Customer'];

    for (Account acc : accountList){
    	// one
    	if(acc.Account_Manager__c == '0051I0000027OCuQAM') {
    		acc.Onboarding_Specialist__c = '0051I0000029k0yQAA';
    		acc.Product_Specialist__c = '0051I0000027ODJQA2';

    		myList.add(acc);
    	}
    	// two
    	if(acc.Account_Manager__c == '0051I0000029k3sQAA') {
    		acc.Onboarding_Specialist__c = '0051I0000029k0ZQAQ';
    		acc.Product_Specialist__c = '0051I000002Q8XaQAK';

    		myList.add(acc);
    	}
    	// three
    	if(acc.Account_Manager__c == '0051I000002baVoQAI') {
    		acc.Onboarding_Specialist__c = '0051I000002slVdQAI';
    		acc.Product_Specialist__c = '0051I000002QShlQAG';

    		myList.add(acc);
    	}
    	// four
    	if(acc.Account_Manager__c == '0051I000002s0dpQAA') {
    		acc.Onboarding_Specialist__c = '0051I000002QPrUQAW';
    		acc.Product_Specialist__c = '0051I0000027ODOQA2';

    		myList.add(acc);
    	}
    	// five
    	if(acc.Account_Manager__c == '0051I000002QPrTQAW') {
    		acc.Onboarding_Specialist__c = '0051I000002s0dqQAA';
    		acc.Product_Specialist__c = '0051I000002stE6QAI';

    		myList.add(acc);
    	}
    }

    update myList;

    }
}
Any help appreciated!
Best Answer chosen by Steven Wellman 28
Amit Chaudhary 8Amit Chaudhary 8
Hi Steven,

Please check below post to learn about scheduler test class
1) http://amitsalesforce.blogspot.com/2017/07/how-to-write-test-class-for-scheduler.html

Try below test class.
@isTest
private class UpdateCSMTeamTes
{

    static testmethod void schedulerTest() 
    {
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        Account acc = new Account();
			acc.name= 'test';
			acc.Type ='Customer';
			acc.Account_Manager__c = userinfo.getuserid(); // you can create user and add id here
        insert acc;
        
        Test.startTest();

            String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, new AccountUpdateBatchJobscheduled());
            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();
		
    }
}

Please post full error here with screen shot.

 

All Answers

Raj VakatiRaj Vakati
Try this and please remove hardcoded record ids in your code 
 
isTest
private class UpdateCSMTeamTest
{

    static testmethod void schedulerTest() 
    {
	
	 Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
		insert uu ; 
        
		
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
		acc.Typ='Customer';
		acc.Account_Manager__c  =uu.Id ;
        insert acc;
        
        Test.startTest();

            String jobId = System.schedule('UpdateCSMTeamTest',  CRON_EXP, new UpdateCSMTeam ());
            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
    }
}

 
Steven Wellman 28Steven Wellman 28
Thanks Raj. I'm planning to remove the Ids once I get it working. That code is throwing a duplicate Id error (the blank one).
Amit Chaudhary 8Amit Chaudhary 8
Hi Steven,

Please check below post to learn about scheduler test class
1) http://amitsalesforce.blogspot.com/2017/07/how-to-write-test-class-for-scheduler.html

Try below test class.
@isTest
private class UpdateCSMTeamTes
{

    static testmethod void schedulerTest() 
    {
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        Account acc = new Account();
			acc.name= 'test';
			acc.Type ='Customer';
			acc.Account_Manager__c = userinfo.getuserid(); // you can create user and add id here
        insert acc;
        
        Test.startTest();

            String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, new AccountUpdateBatchJobscheduled());
            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();
		
    }
}

Please post full error here with screen shot.

 
This was selected as the best answer
Steven Wellman 28Steven Wellman 28
That worked Amit, thank you!