日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Use Batch Apex

發(fā)布時間:2025/4/16 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Use Batch Apex 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.



如果您有很多記錄要處理,例如數(shù)據(jù)清理或歸檔,則Batch Apex可能是您最好的解決方案。



假設(shè)您要使用Batch Apex處理100萬條記錄。

對于要處理的每批記錄,一次調(diào)用批處理類的執(zhí)行邏輯。

每次調(diào)用批處理類時,該作業(yè)都會被放置在Apex作業(yè)隊列中,并作為離散事務(wù)執(zhí)行。

此功能有兩個很棒的優(yōu)點:







在批處理Apex中使用狀態(tài)

Batch Apex通常是無狀態(tài)的。批處理Apex作業(yè)的每次執(zhí)行均被視為離散事務(wù)。

例如,一個包含1,000條記錄并使用默認(rèn)批處理大小的批處理Apex作業(yè)被視為5個事務(wù),每個事務(wù)200條記錄。



假設(shè)您有一項業(yè)務(wù)要求,其中規(guī)定,美國公司的所有聯(lián)系人都必須以其母公司的Account 地址作為其郵寄地址。

不幸的是,用戶正在輸入沒有正確地址的新聯(lián)系人!

編寫一個Batch Apex類,以確保強制執(zhí)行此要求。

global class UpdateContactAddresses implements Database.Batchable<sObject>, Database.Stateful {// instance member to retain state across transactionsglobal Integer recordsProcessed = 0;global Database.QueryLocator start(Database.BatchableContext bc) {return Database.getQueryLocator('SELECT ID, BillingStreet, BillingCity, BillingState, ' +'BillingPostalCode, (SELECT ID, MailingStreet, MailingCity, ' +'MailingState, MailingPostalCode FROM Contacts) FROM Account ' + 'Where BillingCountry = \'USA\'');}global void execute(Database.BatchableContext bc, List<Account> scope){// process each batch of recordsList<Contact> contacts = new List<Contact>();for (Account account : scope) {for (Contact contact : account.contacts) {contact.MailingStreet = account.BillingStreet;contact.MailingCity = account.BillingCity;contact.MailingState = account.BillingState;contact.MailingPostalCode = account.BillingPostalCode;// add contact to list to be updatedcontacts.add(contact);// increment the instance member counterrecordsProcessed = recordsProcessed + 1;}}update contacts;} global void finish(Database.BatchableContext bc){System.debug(recordsProcessed + ' records processed. Shazam!');AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.EmailFROM AsyncApexJobWHERE Id = :bc.getJobId()];// call some utility to send emailEmailUtils.sendMessage(job, recordsProcessed);} }

測試批處理Apex

由于Apex開發(fā)和測試是并行進行的

@isTest private class UpdateContactAddressesTest {@testSetup static void setup() {List<Account> accounts = new List<Account>();List<Contact> contacts = new List<Contact>();// insert 10 accountsfor (Integer i=0;i<10;i++) {accounts.add(new Account(name='Account '+i, billingcity='New York', billingcountry='USA'));}insert accounts;// find the account just inserted. add contact for eachfor (Account account : [select id from account]) {contacts.add(new Contact(firstname='first', lastname='last', accountId=account.id));}insert contacts;}static testmethod void test() { Test.startTest();UpdateContactAddresses uca = new UpdateContactAddresses();Id batchId = Database.executeBatch(uca);Test.stopTest();// after the testing stops, assert records were updated properlySystem.assertEquals(10, [select count() from contact where MailingCity = 'New York']);}}



Create an Apex class that uses Batch Apex to update Lead records

LeadProcessor.apxc

global class LeadProcessor implements Database.Batchable<Sobject> {global Database.QueryLocator start(Database.BatchableContext bc) {return Database.getQueryLocator([Select LeadSource From Lead ]);}global void execute(Database.BatchableContext bc, List<Lead> scope){for (Lead Leads : scope) {Leads.LeadSource = 'Dreamforce';}update scope;} global void finish(Database.BatchableContext bc){ } }

LeadProcessorTest.apxc

@isTest public class LeadProcessorTest {static testMethod void testMethod1(){List<Lead> lstLead = new List<Lead>();for(Integer i=0 ;i <200;i++){Lead led = new Lead();led.FirstName = 'FirstName';led.LastName = 'LastName' +i;led.Company = 'demo' +i;lstLead.add(led);}insert lstLead;Test.startTest();LeadProcessor obj = new LeadProcessor();Database.executeBatch(obj);Test.stopTest();} }

?

總結(jié)

以上是生活随笔為你收集整理的Use Batch Apex的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。