-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMassUpdate.cls
132 lines (110 loc) · 3.9 KB
/
MassUpdate.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
global class MassUpdate extends BatchableSchedulableProcessStep implements Database.Stateful {
global String parameters;
public String query,
sourceField,
targetField;
public Object value;
public Map<String,Object> valuesByField,
sourceFieldsByTargetField;
public String mode;
global override Database.QueryLocator start(Database.BatchableContext btx){
// Attempt to retrieve parameters from our Job record
// if we do not have parameters yet.
if (parameters == null) parameters = params();
if (parameters != null) {
// We expect our parameters to be a JSON object,
// so deserialize it
Map<String,Object> paramsObj;
try {
paramsObj = (Map<String,Object>) JSON.deserializeUntyped(parameters);
query = (String) paramsObj.get('query');
mode = (String) paramsObj.get('mode');
// Default mode is 'Field from Field'
if (mode != null) {
// Retrieve appropriate parameters
// depending on what mode we are dealing with
// FIELD_WITH_VALUE
// For each record retrieved in the query,
// update the specified field
// with the specified value.
if (mode.equalsIgnoreCase('FIELD_WITH_VALUE')) {
targetField = (String) paramsObj.get('field');
value = paramsObj.get('value');
}
// FIELDS_WITH_VALUES
// For each record retrieved in the query,
// update the specified fields
// with corresponding values
else if (mode.equalsIgnoreCase('FIELDS_WITH_VALUES')) {
valuesByField = (Map<String,Object>) paramsObj.get('valuesByField');
}
// FIELD_FROM_FIELD
// For each record retrieved in the query,
// update the specified targetField
// with the value of the row's corresponding sourceField
else if (mode.equalsIgnoreCase('FIELD_FROM_FIELD')) {
sourceField = (String) paramsObj.get('sourceField');
targetField = (String) paramsObj.get('targetField');
}
// FIELDS_FROM_FIELDS
// For each record retrieved in the query,
// update each provided field with the value of another corresponding field.
else if (mode.equalsIgnoreCase('FIELDS_FROM_FIELDS')) {
sourceFieldsByTargetField = (Map<String,Object>) paramsObj.get('sourceFieldsByTargetField');
}
query = (String) paramsObj.get('query');
}
} catch (Exception ex) {
// Complete our batch process
complete();
throw ex;
}
}
if ((query != null) && (mode != null)) {
return Database.getQueryLocator(query);
} else {
// Return a dummy query locator
return Database.getQueryLocator([select Id from User where Id = :UserInfo.getUserId() limit 0]);
}
}
global override void execute(Database.BatchableContext btx, List<SObject> scope) {
for (Sobject so : scope) {
//try {
if (mode.equalsIgnoreCase('FIELD_WITH_VALUE')) {
so.put(targetField,value);
} else if (mode.equalsIgnoreCase('FIELD_FROM_FIELD')) {
so.put(targetField,so.get(sourceField));
} else if (mode.equalsIgnoreCase('FIELDS_WITH_VALUES')) {
for (String field : valuesByField.keyset()){
so.put(field,valuesByField.get(field));
}
} else if (mode.equalsIgnoreCase('FIELDS_FROM_FIELDS')) {
for (String targetField : sourceFieldsByTargetField.keyset()){
so.put(
// Target field
targetField,
// Value of source field on this row
so.get(
String.valueOf(
sourceFieldsByTargetField.get(targetField)
)
)
);
}
}
//} catch (Exception ex) {
// TODO: send detailed error messages
// with the results of each operation
//}
}
Database.update(scope,false);
}
global override void finish(Database.BatchableContext btx) {
// Continue our Batch Process, if we need to
complete();
}
// Implements Schedulable interface
global override void execute(SchedulableContext ctx) {
Database.executeBatch(new MassUpdate());
}
}