Asynchronous Apex 05 - 비동기 Apex

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');
    }
    
}

좋은 웹페이지 즐겨찾기