Asynchronous Apex 03
9283 ワード
🎯 Challenge
LeadProcessorTest.apex
@isTest
public class LeadProcessorTest {
@isTest
public static void testit() {
List<Lead> leads = new List<Lead>();
// insert 200 Lead records
for (Integer i=0;i<200;i++) {
Lead l = new Lead();
l.LastName = 'name' + i;
l.Company = 'Company';
l.Status = 'Random Status';
leads.add(l);
}
insert leads;
//이 사이에서 해야한다
Test.startTest();
LeadProcessor lp = new LeadProcessor();
Id batchId = Database.executeBatch(lp);
Test.stopTest();
}
}
LeadProcessor.apexglobal class LeadProcessor implements Database.Batchable<sObject>{
//stateful은 필요없으니 삭제
Integer count = 0;
//start
global Database.QueryLocator start(Database.BatchableContext bc) {
//collect all Lead update
return Database.getQueryLocator('SELECT ID, LeadSource FROM Lead');
}
global void execute(Database.BatchableContext bc, List<Lead> scope){
LIST<Lead> leads = new LIST<Lead>();
for (Lead lead : scope) {
lead.LeadSource = 'Dreamforce';
leads.add(lead);
count += 1;
}
update leads;
}
global void finish(Database.BatchableContext bc){
System.debug('count = ' + count);
}
}
Reference
この問題について(Asynchronous Apex 03), 我々は、より多くの情報をここで見つけました https://velog.io/@wogus0808/Asynchronous-Apex-03-2i8yo73tテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol