-
Notifications
You must be signed in to change notification settings - Fork 25
/
MassDelete.cls
52 lines (41 loc) · 1.38 KB
/
MassDelete.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
global class MassDelete extends BatchableSchedulableProcessStep implements Database.Stateful {
global String parameters;
public String query;
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');
} catch (Exception ex) {
// Complete our batch process
complete();
throw ex;
}
}
if (query != 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) {
if (scope != null && !scope.isEmpty()){
Database.delete(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 MassDelete());
}
}