-
Notifications
You must be signed in to change notification settings - Fork 1
/
BulkTestCont.cls
442 lines (353 loc) · 17.4 KB
/
BulkTestCont.cls
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
public with sharing class BulkTestCont {
public SObject sObj {get;set;}
public List<String> lFieldApiName{get;set;}
public String selectedObject{get;set;}
public Integer noOfRec{get;set;}
public List<SelectOption> availableRecTypes{get;set;}
public ID selectedRecType{get;set;}
//display state housekeeping
//public Boolean isDisabledNoOfRec{get;set;}
public Boolean isVisibleCreate{get;set;}
public Boolean isVisibleUpdate{get;set;}
public Boolean isVisibleDelete{get;set;}
private List<ID> lSobjIds;
private Schema.SObjectType sObjType;
private Boolean hasNameField;
private Set<String>filterSObjects;
public BulkTestCont(){
lFieldApiName=new List<String>();
noOfRec=1;
lSobjIds=new List<ID>();
availableRecTypes=new List<SelectOption>();
hasNameField=false;
filterSObjects=new Set<String>();
filterSObjects.add('Lead');
//isDisabledNoOfRec=false;
isVisibleCreate=false;
isVisibleUpdate=false;
isVisibleDelete=false;
}
public List<String> getFieldNames( Schema.SObjectType s ){
List<String> result = new List<String>() ;
//Get the token for this object
// Schema.SObjectType s = Schema.getGlobalDescribe().get(objName) ;
Map<String, Schema.SObjectField> fMap = s.getDescribe().fields.getMap() ;
List<Schema.SObjectField> fTokens = fMap.values() ;
hasNameField= false;
for( Integer i = 0 ; i < fTokens.size() ; i++ ){
Schema.DescribeFieldResult f = fTokens.get(i).getDescribe() ;
if(f.isHtmlFormatted())
continue;
if('Name'.equalsIgnoreCase(f.getName())){
hasNameField= true;
// System.debug('$$$$$$method$$$'+hasNameField);
}
result.add(f.getName());
}
result.sort();
return result ;
}
public List<SelectOption> getSObjectNames(){
List<SelectOption> options = new List<SelectOption>();
List<String> lNames=new List<String>();
options.add(new SelectOption('', ''));
for(String name : Schema.getGlobalDescribe().keySet()) {
if (name.endsWithIgnoreCase('history') || name.endsWithIgnoreCase('share') || name.endsWithIgnoreCase('tag')) {
continue;
}
if (name.equalsIgnoreCase('ApexComponent') || name.equalsIgnoreCase('ApexClass') || name.equalsIgnoreCase('ApexTrigger') || name.equalsIgnoreCase('ApexPage') || name.equalsIgnoreCase('ProcessInstance') || name.equalsIgnoreCase('ProcessInstanceHistory') || name.equalsIgnoreCase('ProcessInstanceStep') || name.equals('ProcessInstanceHistory') || name.equalsIgnoreCase('Document') || name.equalsIgnoreCase('DocumentAttachmentMap') || name.equalsIgnoreCase('Note') || name.equals('NoteAndAttachment') ||name.equalsIgnoreCase('EmailServicesAddress') || name.equalsIgnoreCase('EmailServicesFunction') || name.equalsIgnoreCase('EmailStatus') || name.equalsIgnoreCase('FiscalYearSettings') || name.equalsIgnoreCase('Folder') || name.equalsIgnoreCase('ForecastShare') || name.equalsIgnoreCase('Group') || name.equalsIgnoreCase('GroupMember')) {
continue;
}
lNames.add(name.capitalize());
// options.add(new SelectOption(name,name));
}
lNames.sort();
for(String name : lNames){
options.add(new SelectOption(name,name));
}
return options;
}
public PageReference selectObject(){
sObjType=Schema.getGlobalDescribe().get(selectedObject);
// sObj = sObjType.newSObject() ;
// lFieldApiName=getFieldNames(sObjType);
availableRecTypes = getAvailableRecordTypeNamesForSObject(sObjType);
lFieldApiName.clear();
// isDisabledNoOfRec=false;
isVisibleCreate=false;
isVisibleUpdate=false;
isVisibleDelete=false;
return null;
}
public PageReference onSubmit(){
if(!validateOnSubmit()){
return null;
}
ID recTypeID= String.isNotBlank(selectedRecType)?selectedRecType:null;
//System.debug('$$$$$$$$$$$$$$$selectedRecType' +selectedRecType);
sObj = sObjType.newSObject(recTypeID,true) ;
lFieldApiName=getFieldNames(sObjType);
//isDisabledNoOfRec=true;
isVisibleCreate=true;
isVisibleUpdate=false;
isVisibleDelete=false;
return null;
}
private Boolean validateOnSubmit(){
Boolean result=true;
if(String.isBlank(selectedObject)){
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,'Choose an Object'));
result=false;
}
if(noOfRec==null || noOfRec<=0){
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,'Choose a value > 0 for No. of Records'));
result=false;
}
return result;
}
// Returns a List of the Names of all RecordTypes
// available to the running user for a given SOBject type
public List<SelectOption> getAvailableRecordTypeNamesForSObject(Schema.SObjectType objType) {
// List<String> names = new List<String>();
List<SelectOption> options = new List<SelectOption>();
//options.add(new SelectOption('', ''));
List<RecordTypeInfo> infos = objType.getDescribe().getRecordTypeInfos();
for (RecordTypeInfo i : infos) {
if (i.isAvailable() && !String.valueOf(i.getRecordTypeId()).endsWith('AAA')){
options.add(new SelectOption(i.getRecordTypeId(),i.getName()));
}
}
// If there are 2 or more RecordTypes...
/*if (infos.size() > 1) {
for (RecordTypeInfo i : infos) {
if (i.isAvailable()
// Ignore the Master Record Type, whose Id always ends with 'AAA'.
// We check the Id because Name can change depending on the user's language.
&& !String.valueOf(i.getRecordTypeId()).endsWith('AAA')){
options.add(new SelectOption(i.getRecordTypeId(),i.getName()));
//names.add();
}
}
}
// Otherwise there's just the Master record type,
// so add it in, since it MUST always be available
else {
options.add(new SelectOption(infos[0].getRecordTypeId(),infos[0].getName()));
// names.add(infos[0].getName());
}*/
return options;
}
public PageReference createRec(){
lSobjIds.clear();
List<SObject> lInsSObj=new List<SObject>();
for(Integer i=0;i<noOfRec;i++){
SObject obj=sObj.clone(false, true);
System.debug('$$$$$$createRec$$$'+hasNameField);
if(hasNameField && !filterSObjects.contains(selectedObject)){
String s=i+'';
obj.put('Name', obj.get('Name')+s );
}
lInsSObj.add(obj);
}
Database.SaveResult[] srList =null;
Long startTime=null;
Long endTime=null;
Long transTime=null;
GovernorLimits startLimit=null;
GovernorLimits endLimit=null;
try{
startLimit=new GovernorLimits(true);
startTime=DateTime.now().getTime();
srList =Database.insert(lInsSObj,true);
endTime=DateTime.now().getTime();
transTime=(endTime-startTime);
endLimit=new GovernorLimits(true);
}catch(Exception e){
Apexpages.addMessages(e);
}
if(srList==null)
return null;
for (Database.SaveResult sr : srList) {
if(sr.isSuccess()){
lSobjIds.add(sr.getId()) ;
}
else{
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,err.getStatusCode() + ' : ' + err.getMessage()));
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,'Fields that affected this error: ' + err.getFields()));
//System.debug('The following error has occurred.');
//System.debug(err.getStatusCode() + ': ' + err.getMessage());
//System.debug('Fields that affected this error: ' + err.getFields());
}
}
}
if(srList!=null &&srList.size()== lInsSObj.size()){
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,srList.size()+' record(s) created successfully. Transaction time = '+transTime+' ms'));
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,(new GovernorLimits()).displayLimitResults(startLimit,endLimit)));
// isDisabledNoOfRec=true;
isVisibleCreate=false;
isVisibleUpdate=true;
isVisibleDelete=true;
}
return null;
}
public PageReference updateRec(){
List<SObject> lUpdSObj=new List<SObject>();
Integer lIdSize=lSobjIds.size();
for(Integer i=0;i<noOfRec;i++){
SObject obj=sObj.clone(false, true);
if(i<lIdSize){
obj.put('Id', lSobjIds.get(i) );
}
if(hasNameField && !filterSObjects.contains(selectedObject)){
String s=i+'';
obj.put('Name', obj.get('Name')+s );
}
lUpdSObj.add(obj);
}
Database.SaveResult[] srList =null;
Long startTime=null;
Long endTime=null;
Long transTime=null;
GovernorLimits startLimit=null;
GovernorLimits endLimit=null;
try{
startLimit=new GovernorLimits(true);
startTime=DateTime.now().getTime();
srList =Database.update(lUpdSObj,true);
endTime=DateTime.now().getTime();
transTime=(endTime-startTime);
endLimit=new GovernorLimits(true);
}catch(Exception e){
Apexpages.addMessages(e);
}
if(srList==null)
return null;
for (Database.SaveResult sr : srList) {
if(!sr.isSuccess()){
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,err.getStatusCode() + ' : ' + err.getMessage()));
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,'Fields that affected this error: ' + err.getFields()));
// System.debug('The following error has occurred.');
// System.debug(err.getStatusCode() + ': ' + err.getMessage());
// System.debug('Fields that affected this error: ' + err.getFields());
}
}
}
if(srList!=null &&srList.size()== lUpdSObj.size()){
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,srList.size()+' record(s) updated successfully. Transaction time = '+transTime+' ms'));
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,(new GovernorLimits()).displayLimitResults(startLimit,endLimit)));
}
return null;
}
public PageReference deleteRec(){
List<SObject> lDelSObj=new List<SObject>();
Integer lIdSize=lSobjIds.size();
for(Integer i=0;i<noOfRec;i++){
SObject obj= sObjType.newSObject();
if(i<lIdSize){
obj.put('Id', lSobjIds.get(i) );
}
lDelSObj.add(obj);
}
Database.DeleteResult[] srList =null;
Long startTime=null;
Long endTime=null;
Long transTime=null;
GovernorLimits startLimit=null;
GovernorLimits endLimit=null;
try{
startLimit=new GovernorLimits(true);
startTime=DateTime.now().getTime();
srList =Database.delete(lDelSObj,true);
endTime=DateTime.now().getTime();
transTime=(endTime-startTime);
endLimit=new GovernorLimits(true);
}catch(Exception e){
Apexpages.addMessages(e);
}
if(srList==null)
return null;
for (Database.DeleteResult sr : srList) {
if(!sr.isSuccess()){
// lSobjIds.add(sr.getId()) ;
for(Database.Error err : sr.getErrors()) {
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,err.getStatusCode() + ' : ' + err.getMessage()));
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,'Fields that affected this error: ' + err.getFields()));
// System.debug('The following error has occurred.');
// System.debug(err.getStatusCode() + ': ' + err.getMessage());
// System.debug('Fields that affected this error: ' + err.getFields());
}
}
}
if(srList!=null &&srList.size()== lDelSObj.size()){
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,srList.size()+' record(s) deleted successfully. Transaction time = '+transTime+' ms'));
Apexpages.addMessage(new ApexPages.Message(ApexPages.severity.INFO,(new GovernorLimits()).displayLimitResults(startLimit,endLimit)));
//isDisabledNoOfRec=false;
isVisibleCreate=false;
isVisibleUpdate=false;
isVisibleDelete=false;
lSobjIds.clear();
lFieldApiName.clear();
}
return null;
}
class GovernorLimits{
Integer aggregateQueries;
Integer callouts;
Integer cpuTime;
Integer dMLRows;
Integer dMLStatements;
Integer emailInvocations;
Integer futureCalls;
Integer heapSize;
Integer mobilePushApexCalls;
Integer soqlQueries;
Integer queryLocatorRows;
Integer soqlqueryRows;
Integer queueableJobs;
Integer soslQueries;
GovernorLimits(Boolean autoSet){
if(autoSet){
aggregateQueries= Limits.getAggregateQueries();
callouts= Limits.getCallouts();
cpuTime= Limits.getCpuTime();
dMLRows= Limits.getDMLRows();
dMLStatements= Limits.getDMLStatements();
emailInvocations= Limits.getEmailInvocations();
futureCalls= Limits.getFutureCalls();
heapSize= Limits.getHeapSize();
mobilePushApexCalls= Limits.getMobilePushApexCalls();
soqlQueries= Limits.getQueries();
queryLocatorRows= Limits.getQueryLocatorRows();
soqlqueryRows= Limits.getQueryRows();
queueableJobs= Limits.getQueueableJobs();
soslQueries= Limits.getSoslQueries();
}
}
GovernorLimits(){
}
String displayLimitResults(GovernorLimits startLimit,GovernorLimits endLimit){
String result='';
if(startLimit==null || endLimit==null)
return result;
result+= 'Number of SOQL queries: '+(endLimit.soqlQueries-startLimit.soqlQueries)+' out of '+Limits.getLimitQueries()+'<br/>';
result+= 'Number of query rows: '+(endLimit.soqlqueryRows-startLimit.soqlqueryRows)+' out of '+Limits.getLimitQueryRows()+'<br/>';
result+= 'Maximum CPU time: '+(endLimit.cpuTime-startLimit.cpuTime)+' out of '+Limits.getLimitCpuTime()+'<br/>';
result+= 'Number of DML rows: '+(endLimit.dMLRows-startLimit.dMLRows)+' out of '+Limits.getLimitDMLRows()+'<br/>';
result+= 'Number of DML statements: '+(endLimit.dMLStatements-startLimit.dMLStatements)+' out of '+Limits.getLimitDMLStatements()+'<br/>';
result+= 'Maximum heap size: '+(endLimit.heapSize-startLimit.heapSize)+' out of '+Limits.getLimitHeapSize()+'<br/>';
result+= 'Number of queueable jobs added to the queue: '+(endLimit.queueableJobs-startLimit.queueableJobs)+' out of '+Limits.getLimitQueueableJobs()+'<br/>';
result+= 'Number of Webservice callouts: '+(endLimit.callouts-startLimit.callouts)+' out of '+Limits.getLimitCallouts()+'<br/>';
result+= 'Number of Email Invocations: '+(endLimit.emailInvocations-startLimit.emailInvocations)+' out of '+Limits.getLimitEmailInvocations()+'<br/>';
result+= 'Number of future calls: '+(endLimit.futureCalls-startLimit.futureCalls)+' out of '+Limits.getLimitFutureCalls()+'<br/>';
result+= 'Number of aggregate queries: '+(endLimit.aggregateQueries-startLimit.aggregateQueries)+' out of '+Limits.getLimitAggregateQueries()+'<br/>';
result+= 'Number of Mobile Apex push calls: '+(endLimit.mobilePushApexCalls-startLimit.mobilePushApexCalls)+' out of '+Limits.getLimitMobilePushApexCalls()+'<br/>';
result+= 'Number of Database.getQueryLocator() rows: '+(endLimit.queryLocatorRows-startLimit.queryLocatorRows)+' out of '+Limits.getLimitQueryLocatorRows()+'<br/>';
result+= 'Number of SOSL queries: '+(endLimit.soslQueries-startLimit.soslQueries)+' out of '+Limits.getLimitSoslQueries()+'<br/>';
return result;
}
}
}