高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
global class RentalApplyDailyBatch implements Database.Batchable<sObject>, Database.Stateful {
    global List<String> errorList = new List<String>();
    global Integer allCount = 0; // 总明细条目
    global Integer allCountDone = 0;
    global Integer updatedCount = 0; // 实际更新的条目
    global Integer round = 4; // 跑几次
 
    Boolean IsNeedExecute = false; // 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  是否符合执行条件
 
    global RentalApplyDailyBatch() {}
    global RentalApplyDailyBatch(Integer rd) {
        round = rd;
    }
 
    // 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
    global RentalApplyDailyBatch(Boolean NeedExecute) {
        this.IsNeedExecute = NeedExecute;
    }
    // 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  end
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([
            SELECT Salesdept__c
                 , Work_Location__c
                 , Owner_province__c
                 , Branch__c
                 , Onwer_job_category__c
                 , Salesdepartment__c
                 , Salesdept_text_Update_F__c
                 , Owner_Info_text_Update_F__c
              FROM Rental_Apply__c
             WHERE Salesdept_text_Update_F__c = true OR Owner_Info_text_Update_F__c = true
              ]);
    }
 
    global void execute(Database.BatchableContext BC, List<Rental_Apply__c> raList) {
        allCount += raList.size();
        Savepoint sp = Database.setSavePoint();
        try {
            for(Rental_Apply__c ra:raList) {
                if(ra.Salesdept_text_Update_F__c) {
                    ra.Salesdept_text__c = ra.Salesdept__c;
                }
                if(ra.Owner_Info_text_Update_F__c) {
                    ra.Work_Location_text__c      = ra.Work_Location__c;
                    ra.Owner_province_text__c     = ra.Owner_province__c;
                    ra.Branch_text__c             = ra.Branch__c;
                    ra.Onwer_job_category_text__c = ra.Onwer_job_category__c;
                    ra.Salesdepartment_text__c    = ra.Salesdepartment__c;
                }
            }
            Database.update(raList);
            updatedCount += raList.size();
            allCountDone += raList.size();
        }
        catch (Exception e) {
            Database.rollback(sp);
            errorList.add(e.getMessage() + '\n' + e.getStackTraceString());
            System.debug(LoggingLevel.ERROR, e.getMessage() + '\n' + e.getStackTraceString());
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        round -= 1;
        if (allCount != allCountDone || errorList.size() > 0) {
            // 出现任何错误都重跑
            if (round > 0) {
                System.debug(round);
                RentalApplyDailyBatch bat = new RentalApplyDailyBatch(round);
                Database.executeBatch(bat);
            }
            else {
                BatchEmailUtil be = new BatchEmailUtil();
                String[] toList = new String[]{};
                String title = '申请者信息text字段更新batch失败';
                String[] ccList = new String[]{};
                toList.add('shuo_wang@olympus.com.cn');
                for (OrgWideEmailAddress tmpEmailObj : [SELECT Id, Address, DisplayName
                        FROM OrgWideEmailAddress
                        WHERE DisplayName like 'BatchNotify']) {
                    toList.add(tmpEmailObj.Address);
                }
                be.failedMail(toList, ccList, title, String.join(this.errorList, '\n'), allCount, allCountDone, errorList.size());
                be.send();
            }
        }
 
       //2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
        if(!Test.isRunningTest() &&IsNeedExecute==true){
            //batch里调用下一个batch时,希望跟原有的Schedule里面传的条数保持一致
            Id execBTId = Database.executebatch(new FixtureSetDetailDailyUpdateBatch(true),200);
        }
        //2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查 end 
 
    }
}