如何在Apex Batch中处理SerialBatchApexRangeChunkHandler内部Salesforce.com错误

时间:2018-05-08 20:08:40

标签: salesforce apex

我正在批量更新几个帐户记录。但是,每当由于某些验证逻辑而导致更新失败时(在此示例中,数字太长),批处理将停留在处理模式中,我必须手动中止它。在日志中我看到操作:SerialBatchApexRangeChunkHandler状态:内部Salesforce.com错误。如果我删除Database.Stateful然后批处理完成但我需要在电子邮件中的错误详细信息,所以我必须实现它。

请帮助我,如果需要更多细节,请告诉我。

global class testBatchSettingsAndDebug implements Database.Batchable<sobject>, Database.Stateful {

    global String[] email= new String[] {'email@domain.com'};
    global List<Database.saveResult> dsrs = new List<Database.saveResult>();
    global String errorMessages {get; set;}
    global integer count=0;
    global List<String> exception_List;

    global testBatchSettingsAndDebug (){
        errorMessages ='';
    }


    //Start
        global Database.QueryLocator start (Database.BatchableContext bc){
            return Database.getQueryLocator('select id from Account limit 10');

        }    
    //Excecute
    global void execute (Database.BatchableContext bc, List<sobject> scope){
        List<Account > accToUpdateLsit = new List<Account >();//List to hold accs to update
        for (sObject objScope: scope) { 
            Account accObj = (Account )objScope ;//type casting from generic sOject to acc
            accObj.NumberofLocations__c= 123456;
            accToUpdateLsit.add(accObj);
         } 

         count=accToUpdateLsit.size();

         System.debug('Number of Records to update: '+accToUpdateLsit.size());



            if (accToUpdateLsit != null && accToUpdateLsit.size()>0) {//Check if List is empty or not
                dsrs = Database.update(accToUpdateLsit, false);
                System.debug('Records are successfully updated '+accToUpdateLsit);//Update the Records
            }




        for(Database.SaveResult dsr : dsrs){
            if(!dsr.isSuccess()){
                errorMessages += string.valueof(dsr.geterrors() + '\n'); 


           }
        }
        system.debug('Error occured: '+errorMessages);




    }

    //Finish
    global void finish (Database.BatchableContext BC){

       System.debug('**Record Updated**'+count);


       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

      //Below code will fetch the job Id
          AsyncApexJob a = [Select a.TotalJobItems, a.Status, a.NumberOfErrors, a.JobType, a.JobItemsProcessed, a.ExtendedStatus, a.CreatedById, a.CompletedDate From AsyncApexJob a WHERE id = :BC.getJobId()];//get the job Id
          System.debug('Jobid is'+BC.getJobId());

          //below code will send an email to User about the status
          mail.setToAddresses(email);
          mail.setReplyTo('email@domain.com');//Add here your email address
          mail.setSenderDisplayName('Account Batch Notification');
          mail.setSubject('Account Batch Batch Processing '+a.Status);
          mail.setPlainTextBody('The Batch Apex job processed:  '+a.TotalJobItems+' batches with: '+a.NumberOfErrors+' failures: '+' Error Messages:'+errorMessages );
          Messaging.sendEmail(new Messaging.Singleemailmessage [] {mail});



       System.debug('**Job completed**');

    }
}

1 个答案:

答案 0 :(得分:0)

看起来你可以做的就像上面提到的那样: 声明你的Database.saveResult als transient