非同期Apex 05-非同期Apex

11702 ワード

Schedule Jobs Using the Apex Scheduler


計画タスク-Apexスケジューラの使用[eodv]

📄 Scheduled Apex Syntax


スケジュールApex構文
public class SomeClass implements Schedulable {
    public void execute(SchedulableContext ctx) {
        // awesome code here
    }
}

📄 Using the System.Schedule Method


System.Scheduleメソッドの使用
RemindOpptyOwners reminder = new RemindOpptyOwners();
// Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
String sch = '20 30 8 10 2 ?'; //??년 2월 10일 8시 30분 20초
String jobID = System.schedule('Remind Opp Owners', sch, reminder);

📄 Scheduling a Job from the UI


UIでのタスクの保持



🎯 Challenge


DaliyLeadProcessor
global class DailyLeadProcessor implements Schedulable { //Schedulable사용 - implements
    global void execute(SchedulableContext ctx){
        LIST<lead> leadstoupdate = new LIST<lead>();
        LIST<Lead> leads = [Select id From Lead
                           Where LeadSource = NULL Limit 200];
        
        for(Lead l : leads){
            l.LeadSource = 'Dreamforce';
            leadstoupdate.add(l);
        }
        update leadstoupdate;
    }
}
DailyLeadProcessorTest
@isTest
private class DailyLeadProcessorTest {
	
    //날짜를 현재보다 뒤로 맞춰야 에러가 발생 안한다
    public static String CRON_EXP = '0 0 0 15 5 ? 2022';
    static testmethod void testScheduledJob(){
        List<Lead> leads = new List<lead>();
        for(Integer i = 0; i < 200; i++){
            Lead l = new Lead(
            	FirstName = 'First ' + i,
                LastName = 'LastName',
                Company = 'The Inc'
            );
            leads.add(l);
        }
        insert leads;
        
        Test.startTest();
        String jobId = System.schedule('SchduledApexTest', 
                                    CRON_EXP, new DailyLeadProcessor());
        Test.stopTest();
        
        LIST<Lead> checkleads = new LIST<Lead>();
        checkleads = [Select Id From Lead Where LeadSource = 'Dreamforce' 
                      and Company = 'The Inc'];
        System.assertEquals(200, checkleads.size(), 'Leads were not created');
    }
    
}