当前位置:网站首页>Salesforce batch apex batch processing (I)
Salesforce batch apex batch processing (I)
2022-06-24 01:12:00 【repick】
Apex SOQL You can only query at most 50000 Data ,DML There is less data that can be manipulated 10000 strip , If you want to process large amounts of data , Consider using Batch Apex function ,Batch Apex Realization Database.batchable Interface
Datebase.Batchable The following three methods are encapsulated :
1.start Method
public (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {} Used to collect the data to be operated , Then send the data to execute() Carry out specific operations , Use SOQL There is no limit on the total amount obtained ,
for example , about Account Come on , Maximum storage 5000 Thousands of pieces of , All can be returned .
2.execute Method
public void execute(Database.BatchableContext BC, list<P>){} Yes start Method to process the incoming data .
3.finish Method
public void finish(Database.BatchableContext BC){}batch After execution, execute , Generally used as email notification , Or subsequent operations .
4.Database.executeBatch Method
Call this method , You can start executing the batch ,
There are two parameters , The first is to be executed Batch Of Class name , The second is the incoming execute Methodical Record Count
5. Actual installation example
global with sharing class ExampleUpdateRecordBatch implements Database.Batchable<sObject>,Database.Stateful,Database.AllowsCallouts {
public ExampleUpdateRecordBatch() {
}
global Database.QueryLocator start(Database.BatchableContext BC) {
String queryS = 'SELECT Id,Name FROM Opportunity WHERE DeleteFlg__c = true';
return Database.getQueryLocator(queryS);
}
global void execute(Database.BatchableContext BC, list<sObject> scope) {
Savepoint sp = Database.setSavepoint();
try {
List<Opportunity> newOppList = new List<Opportunity>();
for(Opportunity opportunityItem : (List<Opportunity>)scope) {
opportunityItem.stageName = 'Closed Won';
newOppList.add(opportunityItem);
}
update newOppList;
} catch (Exception ex) {
Database.rollback(sp);
}
}
global void finish(Database.BatchableContext BC) {
}
}
Batch perform : In order to test , We execute the following code in the anonymous box
ExampleUpdateRecordBatch batchTest = new ExampleUpdateRecordBatch(); Database.executeBatch(batchTest, 2000);
Execution results :
6. Test class
@IsTest
private with sharing class ExampleUpdateRecordBatchTest {
@TestSetup
static void initialOrgInfo(){
String strUserName = 'BatchTest001';
Profile profile = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
User batchTestUser = new User();
batchTestUser.ProfileId = profile.Id;
batchTestUser.UserName = strUserName + '@sample.com';
batchTestUser.FirstName = '';
batchTestUser.LastName = strUserName;
batchTestUser.EMail = '[email protected]';
batchTestUser.Alias = 'testuser';
batchTestUser.TimeZoneSidKey = 'Asia/Tokyo';
batchTestUser.LocaleSidKey = 'ja_JP';
batchTestUser.EmailEncodingKey = 'ISO-2022-JP';
batchTestUser.LanguageLocaleKey = 'ja';
Database.insert(batchTestUser, false);
PermissionSet permissionSet = [SELECT Id
FROM PermissionSet
WHERE Name = 'Ursus_Park_User'];
PermissionSetAssignment assignmentAss = new PermissionSetAssignment();
assignmentAss.AssigneeId = batchTestUser.Id;
assignmentAss.PermissionSetId = permissionSet.Id;
Database.insert(assignmentAss, false);
}
@IsTest static void testBatch001() {
User runUser = [SELECT ID, UserName FROM User WHERE username='[email protected]'];
List <Opportunity> oppList = new List<Opportunity>();
for(integer i = 0; i<200; i++){
Opportunity oppItem = new Opportunity(Name='testOpportunity'+ i,
StageName='Perception Analysis',
Ownerid = runUser.Id,
CloseDate = Date.Today(),
DeleteFlg__c = true);
oppList.add(oppItem);
}
insert oppList;
System.runAs(runUser) {
Test.StartTest();
ExampleUpdateRecordBatch reassign = new ExampleUpdateRecordBatch();
ID batchprocessid = Database.executeBatch(reassign, 200);
Test.StopTest();
}
List<Opportunity> oppUpdateList = [SELECT Id,StageName FROM Opportunity WHERE Ownerid = :runUser.Id];
System.assertEquals(200, oppUpdateList.size());
if (oppUpdateList != null && oppUpdateList.size() >0) {
for (Opportunity oppUpdate : oppUpdateList) {
System.assertEquals('Closed Won', oppUpdate.StageName);
}
}
}
}TestClass perform
边栏推荐
- [CVPR 2020] conference version: a physics based noise formation model for extreme low light raw denoising
- [ICPR 2021] tiny object detection in aerial images
- [CVPR 2020 oral] a physics based noise formation model for extreme low light raw denoising
- 实时计算框架:Flink集群搭建与运行机制
- Is it safe to open an account for shares of tongdaxin?
- 【小程序】编译预览小程序时,出现-80063错误提示
- Is it safe to open an account online? What conditions need to be met?
- 用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧
- [OSG] OSG development (04) - create multiple scene views
- ShardingSphere-proxy-5.0.0容量范围分片的实现(五)
猜你喜欢
![[Hongke case] how can 3D data become operable information Object detection and tracking](/img/d8/ccda595db67b66eb01f3d55956f4cb.png)
[Hongke case] how can 3D data become operable information Object detection and tracking

实时计算框架:Flink集群搭建与运行机制

Efficient integration of heterogeneous single cell transcriptome with scanorama

The concept of TP FP TN FN in machine learning

CVPR2022 | 可精简域适应

paddle使用指南

GNN upper edge distributor! Instead of trying to refine pills, you might as well give your GNN some tricks

Installation and use of winscp and putty

LSF opens job idle information to view the CPU time/elapse time usage of the job

【ICPR 2021】遥感图中的密集小目标检测:Tiny Object Detection in Aerial Images
随机推荐
jdbc
Perhaps the greatest romance of programmers is to commemorate their dead mother with a software
version `ZLIB_1.2.9‘ not found (required by /lib64/libpng16.so.16)
分别用SVM、贝叶斯分类、二叉树、CNN实现手写数字识别
Version ` zlib 1.2.9 "not found (required by / lib64 / libpng16.so.16)
[CVPR 2020 oral] a physics based noise formation model for extreme low light raw denoising
Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
阿里巴巴面试题:多线程相关
Solve the problem that Base64 compressed files are extracted with spaces after post request
Common core resource objects of kubernetes
Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)
[planting grass by technology] 13 years' record of the prince of wool collecting on the cloud moving to Tencent cloud
Architecture solutions
version `ZLIB_ 1.2.9‘ not found (required by /lib64/libpng16.so.16)
13 `bs_ duixiang. Tag tag ` get a tag object
numpy. linalg. Lstsq (a, B, rcond=-1) parsing
2021-11-21: map[i][j] = = 0, which means that (I, J) is an ocean. If you cross it, the cost will be
An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
Part of the problem solution of unctf2020
Is it safe to open a stock account online now? Select a state-owned securities firm, and the fastest time to open an account is 8 minutes