日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Use Batch Apex

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



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



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

對于要處理的每批記錄,一次調用批處理類的執行邏輯。

每次調用批處理類時,該作業都會被放置在Apex作業隊列中,并作為離散事務執行

此功能有兩個很棒的優點:







在批處理Apex中使用狀態

Batch Apex通常是無狀態的。批處理Apex作業的每次執行均被視為離散事務。

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



假設您有一項業務要求,其中規定,美國公司的所有聯系人都必須以其母公司的Account 地址作為其郵寄地址。

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

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

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開發和測試是并行進行的

@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();} }

?

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。