高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
     * @Author    lijinhuan
     * @DateTime  2022-12-09T14:22:31+0800
     * 测试类:MergeObjectBatchTest
     */
global class MergeObjectBatch implements Database.Batchable<sObject> {
    public String query;
    public Set<Id> errorIdSet;
    public String logstr = '';
    public String errorstr = '';
    public String sObjectName;
    public String selectStr;
    global MergeObjectBatch() {
        this.query = query;
    }
    global MergeObjectBatch(String selectStr,String sObjectName){
        this.query = query;
        this.sObjectName = sObjectName;
        this.selectStr = selectStr;
        logstr = logstr + sObjectName + '\n开始';
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([
            SELECT Id,master__c 
              FROM MergeObject__c
             WHERE mergeFinish__c = false
              ]);
    }
 
    global void execute(Database.BatchableContext BC, list<MergeObject__c> MergeObject) {
        logstr += 'zheli:';
        //默认只执行一个
        if(MergeObject.size() > 0 && String.isNotBlank(selectStr) && String.isNotBlank(sObjectName)){
            SObject master;
            List<SObject> duplicates;
            if(sObjectName.equalsIgnoreCase('Contact')){
                master = new Contact();
                duplicates = new List<Contact>();
            }
            String masterStr = MergeObject[0].master__c;
            List<String> duplicatesList = MergeObject[0].duplicates__c.split(',');
 
            String masterSoql = selectStr+sObjectName+' where id = :masterStr';
            master = Database.query(masterSoql);
            String dupSoql = selectStr+sObjectName+' where id in :duplicatesList';
            duplicates = Database.query(dupSoql);
            // Merge accounts into master
            Database.MergeResult[] results = Database.merge(master, duplicates, false);
 
            Boolean okFlag = true;
            for(Database.MergeResult res : results) {
                if (res.isSuccess()) {
                    // Get the master ID from the result and validate it
                    System.debug('Master record ID: ' + res.getId());
                    System.assertEquals(master.Id, res.getId());              
                    logstr += ';master.Id'+res.getId();
                    // Get the IDs of the merged records and display them
                    List<Id> mergedIds = res.getMergedRecordIds();
                    System.debug('IDs of merged records: ' + mergedIds);                
                    logstr += ';IDs of merged records:'+mergedIds;
                    // Get the ID of the reparented record and 
                    // validate that this the contact ID.
                    System.debug('Reparented record ID: ' + res.getUpdatedRelatedIds());
                    logstr += ';Reparented record ID:'+res.getUpdatedRelatedIds();            
                }
                else {
                    okFlag = false;
                    for(Database.Error err : res.getErrors()) {
                        // Write each error to the debug output
                        System.debug('zheli:'+err.getMessage());
                        logstr += ';errorMessages:'+err.getMessage();
                        errorstr += ';errorMessages:'+err.getMessage();
                    }
                }
            }
            if(okFlag){
                MergeObject[0].mergeFinish__c = true;
                update MergeObject;
            }
        }
        
    }
 
    global void finish(Database.BatchableContext BC) {
        BatchIF_Log__c batchIfLog = new BatchIF_Log__c();
        batchIfLog.Type__c = 'mergeError';
        logstr += '\nMergeObjectBatchEnd';
        if(errorstr.length() > 60000){
            batchIfLog.ErrorLog__c = errorstr.substring(0,60000);
        }else{
            batchIfLog.ErrorLog__c = errorstr.substring(0,errorstr.length());
        }
        
        if(logstr.length() < 131072){
            batchIfLog.Log__c = logstr.substring(0, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072 && logstr.length() < 131072*2){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*2 && logstr.length() < 131072*3){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*3 && logstr.length() < 131072*4){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*4 && logstr.length() < 131072*5){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*5 && logstr.length() < 131072*6){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*6 && logstr.length() < 131072*7){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*7 && logstr.length() < 131072*8){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*8 && logstr.length() < 131072*9){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*9 && logstr.length() < 131072*10){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = logstr.substring(131072*9, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*10 && logstr.length() < 131072*11){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = logstr.substring(131072*9, 131072*10);
            batchIfLog.Log11__c = logstr.substring(131072*10, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*11 && logstr.length() < 131072*12-5){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = logstr.substring(131072*9, 131072*10);
            batchIfLog.Log11__c = logstr.substring(131072*10, 131072*11);
            batchIfLog.Log12__c = logstr.substring(131072*11, logstr.length());
        }
        if(logstr.length() > 131072*12-5){
            logstr += '...';
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = logstr.substring(131072*9, 131072*10);
            batchIfLog.Log11__c = logstr.substring(131072*10, 131072*11);
            batchIfLog.Log12__c = logstr.substring(131072*11, logstr.length());
        }
        insert batchIfLog;
    }
}