当前位置:网站首页>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

原网站

版权声明
本文为[repick]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211120085508833h.html