每当执行批处理作业时,如何将记录的 IsProcessed 标志设置为 True?

How To Make IsProcessed Flag Is Set To True for records whenever a batch Job is executed?

在我的自定义对象 ERT_Case_Type__c 中,我有一个 IsProcessed 复选框,默认情况下为 false

现在我需要在执行批处理作业时将此 IsProcessed 标志设置为 True,这是伪代码,现在我正在寻找的是,我需要在下面的伪批处理 apex 代码中进行哪些确切更改以使此代码在处理每个批次后将 IsProcessed Flag 设置为 True

全局 class copyertbatch6am 实现 Database.Batchable {

                global Database.QueryLocator start(Database.BatchableContext BC) {
                    // collect the batches of records or objects to be passed to execute
                    
                    String query = 'select Case__c, Level_1__c, Level_2__c,Level_3__c  FROM ERT_Case_Type__c where createddate = today and IsProcessed Flag = False';
                    System.debug('ERT Case No is =====>' +query);
                    return Database.getQueryLocator(query);
                }
                
                global void execute(Database.BatchableContext BC, List<ERT_Case_Type__c> exeList) {
                   
                    // process each batch of records
            List<Case_Type__c> listCTD = new List<Case_Type__c>();
                    System.debug('ERT Case No is =====>' +exeList);
                    for(ERT_Case_Type__c exe : exeList)
                    {        
                        listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
                       IsProcessed Flag = True
                    }
                    try {
                        System.debug('ERT Case No is =====>' +listCTD);
                        insert listCTD;
                    
                    } catch(Exception e) {
                        System.debug(e);
                    }
                    
                }   
                
                global void finish(Database.BatchableContext BC) {
                    // execute any post-processing operations
              }
            }

非常感谢您的帮助

感谢和问候,

卡罗琳

在这种情况下,我会实施 Database.Stateful。抱歉,我不喜欢换行符。

              global class ertcopybatch3pm implements Database.Batchable<sObject>, Database.Stateful {
                        private List<ERT_Case_Type__c> processedRecords;
                    global Database.QueryLocator start(Database.BatchableContext BC) {
                        processedRecords = new List<ERT_Case_Type__c>();
                        // collect the batches of records or objects to be passed to execute        
                        String query = 'select Case__c, Level_1__c, Level_2__c,Level_3__c  FROM ERT_Case_Type__c where createddate = today and IsProcessed__c = False';
                        System.debug('ERT Case No is =====>' +query);
                        return Database.getQueryLocator(query);
                    }
                    global void execute(Database.BatchableContext BC, List<ERT_Case_Type__c> exeList) {   
                        // process each batch of records
                        List<Case_Type__c> listCTD = new List<Case_Type__c>();
                        System.debug('ERT Case No is =====>' +exeList);
                        for(ERT_Case_Type__c exe : exeList)
                        {        
                            listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
                           exe.IsProcessed__c = true;
                        }
                        try {
                            System.debug('ERT Case No is =====>' +listCTD);
                            insert listCTD;
                            //only successful batches will be processed in the finish() method
                            processedRecords.addAll(exeList);
                        } catch(Exception e) {
                            System.debug(e);
                        }
                    }   
                    global void finish(Database.BatchableContext BC) {
                        // execute any post-processing operations
                        update processedRecords;
                    }
                }
  1. 在 class 定义中实现 Database.Stateful
  2. 声明一个私有 属性 以在 class 实例化时存储处理过的记录
  3. 在 start() 方法中初始化一个空列表,以便您可以向其中添加项目
  4. 在 execute() 方法中为成功处理的记录设置标志并将它们添加到列表中
  5. 在 finish() 方法中一次更新所有批次中所有成功处理的记录