当前位置:网站首页>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
边栏推荐
- [machine learning] linear regression prediction
- [CVPR 2022] high resolution small object detection: cascaded sparse query for accelerating high resolution smal object detection
- Server performance monitoring: Best Practices for server monitoring
- Empty encoded password warning reason
- 杂乱的知识点
- Sockfwd a data forwarding gadget
- [technology planting grass] skillfully use cloud function to create wechat web page authorization public service
- Solve the problem that Base64 compressed files are extracted with spaces after post request
- What problems need to be solved by MES management system in the era of intelligent manufacturing
- Gin framework: automatically add requestid
猜你喜欢

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

13 `bs_duixiang.tag标签`得到一个tag对象

数据管理:业务数据清洗,落地实现方案

Social recruitment interview is indispensable -- 1000 interview questions for Android engineers from Internet companies

C language: sorting with custom functions

Use recursion to form a multi-level directory tree structure, with possibly the most detailed notes of the whole network.

Cvpr2022 𞓜 thin domain adaptation
![[applet] when compiling the preview applet, a -80063 error prompt appears](/img/4e/722d76aa0ca3576164fbed4e2c4db2.png)
[applet] when compiling the preview applet, a -80063 error prompt appears

Application configuration management, basic principle analysis

CVPR2022 | 可精简域适应
随机推荐
Esp8266 OTA remote and wireless upgrade
【机器学习】线性回归预测
One article introduces you to the world of kubernetes
C language: how to solve the problem of hundreds of horses and loads
What problems need to be solved by MES management system in the era of intelligent manufacturing
Real time computing framework: Spark cluster setup and introduction case
November 20, 2021: the start and end times of a movie can be listed in a small array
Cvpr2022 𞓜 thin domain adaptation
JS input / output statements, variables
Architecture solutions
【SPRS J P & RS 2022】小目标检测模块:A Normalized Gaussian Wasserstein Distance for Tiny Object Detection
Handwritten digit recognition using SVM, Bayesian classification, binary tree and CNN
Social recruitment interview is indispensable -- 1000 interview questions for Android engineers from Internet companies
【小程序】编译预览小程序时,出现-80063错误提示
Devops culture: Amazon leadership principles
Is it safe to open an account for shares of tongdaxin?
阿里巴巴面试题:多线程相关
Cross domain and jsonp
Isn't this another go bug?
Pure JS implementation determines whether the IP is pinged