-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathUtil_DML_Operations.cls
More file actions
187 lines (164 loc) · 6.24 KB
/
Util_DML_Operations.cls
File metadata and controls
187 lines (164 loc) · 6.24 KB
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
/**
* @description This utility class is intended to become a single point (where appropriate) for DML operations to occur.
* The primary purpose of this is to ensure proper logging, error handling and reduction in code duplication.
*
*@author Matt Gerry matt.e.gerry@accenturefederal.com
*@date 7/13/2020
*/
public with sharing class Util_DML_Operations
{
private static final String SUCCESS = 'Success';
/**
* @description Takes list of SObjects and attempts an "AllOrNone = true" (default operation) delete operation.
* @example deleteOperationAllOrNone(SObjectListToDelete, 'theCallingClass', 'theCallingMethod'
* @return String
*/
public String deleteOperationAllOrNone(List<SObject> deletionList, String callingClass, String callingMethod)
{
try
{
database.delete(deletionList);
return SUCCESS;
}
catch(Exception except)
{
Util_Error_Logger.insertNewErrorLog(except, callingClass, callingMethod);
return except.getMessage();
}
}
/**
* @description Takes list of SObjects and attempts an "AllOrNone = false" delete operation.
* @example deleteOperationAllOrNoneFalse(SObjectListToDelete, 'theCallingClass', 'theCallingMethod'
* @return string
*/
public String deleteOperationAllOrNoneFalse(List<SObject> deletionList, String callingClass, String callingMethod)
{
System.debug('DeletionList: ' + deletionList);
Database.DeleteResult[] deleteResult = database.delete(deletionList, false);
String result = processDeleteResult(deleteResult, callingClass, callingMethod);
return result;
}
/**
* @description Takes list of SObjects and attempts an "AllOrNone = true" (default operation) insert operation.
* @example insertOperationAllOrNone(SObjectListToDelete, 'theCallingClass', 'theCallingMethod'
* @return string
*/
public String insertOperationAllOrNone(List<SObject> insertionList, String callingClass, String callingMethod)
{
try
{
database.insert(insertionList);
return SUCCESS;
}
catch(Exception except)
{
Util_Error_Logger.insertNewErrorLog(except, callingClass, callingMethod);
return except.getMessage();
}
}
/**
* @description Takes list of SObjects and attempts an "AllOrNone = false" insert operation.
* @example insertOperationAllOrNoneFalse(SObjectListToDelete, 'theCallingClass', 'theCallingMethod'
* @return string
*/
public String insertOperationAllOrNoneFalse(List<SObject> insertionList, String callingClass, String callingMethod)
{
Database.SaveResult[] saveResults = Database.insert(insertionList, false);
String result = processSaveResult(saveResults, callingClass, callingMethod);
return result;
}
/**
* @description Takes list of SObjects and attempts an "AllOrNone = false" update operation.
* @example updateOperationAllOrNone(SObjectListToDelete, 'theCallingClass', 'theCallingMethod'
* @return string
*/
public String updateOperationAllOrNone(List<SObject> updateList, String callingClass, String callingMethod)
{
try
{
database.update(updateList);
return SUCCESS;
}
catch(Exception except)
{
Util_Error_Logger.insertNewErrorLog(except, callingClass, callingMethod);
return except.getMessage();
}
}
/**
* @description Takes list of SObjects and attempts an "AllOrNone = false" update operation.
* @example updateOperationAllOrNoneFalse(SObjectListToDelete, 'theCallingClass', 'theCallingMethod'
* @return string
*/
public String updateOperationAllOrNoneFalse(List<SObject> updateList, String callingClass, String callingMethod)
{
Database.SaveResult[] saveResults = Database.insert(updateList, false);
String result = processSaveResult(saveResults, callingClass, callingMethod);
return result;
}
/**
* @description Takes a SaveResult aggregation and processes the results whether success of failure, including logging any errors
* @example processSaveResult(saveResultObject, 'The Calling Class', 'The Calling Method')
* @return string
*/
private String processSaveResult(Database.SaveResult[] saveResults, String callingClass, String callingMethod)
{
String returnMessage = SUCCESS;
List<Error_Record_Detail__c> errorRecordDetailsList = new List<Error_Record_Detail__c>();
Error_Log__c loggedError = new Error_Log__c();
Boolean insertParentError = true;
for (Database.SaveResult saveResult : saveResults)
{
if (!saveResult.isSuccess())
{
String errorMessages = '';
if(insertParentError)
{
loggedError = Util_Error_Logger.insertNewErrorLog(null, callingClass, callingMethod);
insertParentError = false;
}
for (Database.Error error : saveResult.getErrors())
{
System.debug('DML Err: ' + String.valueOf(error.getStatusCode()) + ' ' + error.getMessage().left(99));
errorRecordDetailsList.add(Util_Error_Logger.createErrorRecordDetail(loggedError.Id, error, saveResult.getId()));
errorMessages += ' ' + error.getMessage();
}
returnMessage = errorMessages;
}
}
Util_Error_Logger.insertErrorRecordDetails(errorRecordDetailsList);
return returnMessage;
}
/**
* @description Takes a DeleteResult aggregation and processes the results whether success of failure, including logging any errors
* @example processDeleteResult(deleteResultObject, 'The Calling Class', 'The Calling Method')
* @return string
*/
private String processDeleteResult(Database.DeleteResult[] deleteResults, String callingClass, String callingMethod)
{
String returnMessage = SUCCESS;
List<Error_Record_Detail__c> errorRecordDetailsList = new List<Error_Record_Detail__c>();
Error_Log__c loggedError = new Error_Log__c();
Boolean insertParentError = true;
for (Database.DeleteResult deleteResult : deleteResults)
{
if (!deleteResult.isSuccess())
{
String errorMessages = '';
if(insertParentError)
{
loggedError = Util_Error_Logger.insertNewErrorLog(null, callingClass, callingMethod);
insertParentError = false;
}
for (Database.Error error : deleteResult.getErrors())
{
errorRecordDetailsList.add(Util_Error_Logger.createErrorRecordDetail(loggedError.Id, error, deleteResult.getId()));
errorMessages += ' ' + error.getMessage();
}
returnMessage = errorMessages;
}
}
Util_Error_Logger.insertErrorRecordDetails(errorRecordDetailsList);
return returnMessage;
}
}