-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeadStatusBatch.cls
173 lines (156 loc) · 7.45 KB
/
LeadStatusBatch.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
public class LeadStatusBatch implements Database.Batchable<SObject>, Schedulable {
// Start method for Batchable interface
public Database.QueryLocator start(Database.BatchableContext BC) {
// Query all Leads converted with Status 'Converted', ensure they meet the minimum data quality requirements
return Database.getQueryLocator([
SELECT Id,
ConvertedDate,
ConvertedAccount.CreatedDate,
ConvertedContact.CreatedDate,
ConvertedOpportunity.CreatedDate,
Status
FROM Lead
WHERE Status = 'Converted'
AND IsConverted = true
AND ConvertedDate != null
AND ConvertedAccountId != null
AND ConvertedContactId != null
]);
}
// Execute method for Batchable interface
public void execute(Database.BatchableContext BC, List<Lead> scope) {
List<Lead> leadsToUpdate = new List<Lead>();
// List<ErrorLog__c> errorLogs = new List<ErrorLog__c>();
for (Lead ld : scope) {
try {
String newStatus = determineNewStatus(ld);
if (newStatus.startsWith('Error')) {
// Log the error using existing ErrorLog__c object
/*
ErrorLog__c errorLog = new ErrorLog__c(
ClassName__c = 'LeadStatusBatch',
FunctionName__c = 'execute',
ErrorMessage__c = newStatus,
ErrorMessageLong__c = 'Lead ID: ' + ld.Id,
// StackTrace__c can be populated if available
);
errorLogs.add(errorLog);
*/
// TODO: Implement your own error handling mechanism here.
// For example, you could send an email notification, log to a different custom object, or use Platform Events.
System.debug('Error in LeadStatusBatch.execute: ' + newStatus + ' for Lead ID: ' + ld.Id);
} else {
// Update the Lead status
ld.Status = newStatus;
leadsToUpdate.add(ld);
}
} catch (Exception e) {
// Handle unexpected exceptions and log them
/*
ErrorLog__c errorLog = new ErrorLog__c(
ClassName__c = 'LeadStatusBatch',
FunctionName__c = 'execute',
ErrorMessage__c = e.getMessage(),
ErrorMessageLong__c = 'Lead ID: ' + ld.Id + '\n' + e.getStackTraceString(),
StackTrace__c = e.getStackTraceString()
);
errorLogs.add(errorLog);
*/
// TODO: Implement your own exception handling mechanism here.
System.debug('Exception in LeadStatusBatch.execute: ' + e.getMessage() + ' for Lead ID: ' + ld.Id);
}
}
// Perform DML operations outside the loop
if (!leadsToUpdate.isEmpty()) {
try {
update leadsToUpdate;
} catch (DmlException d) {
// Log DML exceptions
for (Lead failedLead : leadsToUpdate) {
/*
ErrorLog__c errorLog = new ErrorLog__c(
ClassName__c = 'LeadStatusBatch',
FunctionName__c = 'execute',
ErrorMessage__c = 'DML Exception',
ErrorMessageLong__c = 'Failed to update Lead ID: ' + failedLead.Id + '\n' + d.getMessage(),
StackTrace__c = d.getStackTraceString()
);
errorLogs.add(errorLog);
*/
// TODO: Implement your own DML exception handling mechanism here.
System.debug('DML Exception in LeadStatusBatch.execute: Failed to update Lead ID: ' + failedLead.Id + ' - ' + d.getMessage());
}
}
}
/*
if (!errorLogs.isEmpty()) {
try {
insert errorLogs;
} catch (DmlException d) {
System.debug('Failed to insert error logs: ' + d.getMessage());
}
}
*/
// Note: Since ErrorLog__c is commented out, ensure you have your own error handling implemented.
}
// Finish method for Batchable interface
public void finish(Database.BatchableContext BC) {
// Optionally, implement post-processing like sending notifications
}
// Helper method to determine the new status based on the provided logic
private String determineNewStatus(Lead ld) {
DateTime convertedDate = ld.ConvertedDate;
DateTime accountCreatedDate = ld.ConvertedAccount.CreatedDate.date();
DateTime contactCreatedDate = ld.ConvertedContact.CreatedDate.date();
DateTime oppCreatedDate = ld.ConvertedOpportunity != null ? ld.ConvertedOpportunity.CreatedDate.date() : null;
Boolean isExAcc = accountCreatedDate != null && accountCreatedDate != convertedDate;
Boolean isNewAcc = accountCreatedDate == convertedDate;
Boolean isExCon = contactCreatedDate != null && contactCreatedDate != convertedDate;
Boolean isNewCon = contactCreatedDate == convertedDate;
if (isNewAcc) {
if (isNewCon) {
if (oppCreatedDate == null) {
return 'Converted - New Acc, New Con, No Opp';
} else if (oppCreatedDate == convertedDate) {
return 'Converted - New Acc, New Con, New Opp';
} else {
return 'Error - not converting into existing Opps';
}
} else if (isExCon) {
if (oppCreatedDate == null) {
return 'Converted - New Acc, Ex Con, No Opp';
} else if (oppCreatedDate == convertedDate) {
return 'Converted - New Acc, Ex Con, New Opp';
} else {
return 'Error - not converting into existing Opps';
}
}
} else if (isExAcc) {
if (isNewCon) {
if (oppCreatedDate == null) {
return 'Converted - Ex Acc, New Con, No Opp';
} else if (oppCreatedDate == convertedDate) {
return 'Converted - Ex Acc, New Con, New Opp';
} else {
return 'Converted - Ex Acc, New Con, Ex Opp';
}
} else if (isExCon) {
if (oppCreatedDate == null) {
return 'Converted - Ex Acc, Ex Con, No Opp';
} else if (oppCreatedDate == convertedDate) {
return 'Converted - Ex Acc, Ex Con, New Opp';
} else {
return 'Converted - Ex Acc, Ex Con, Ex Opp';
}
}
}
// Default case if none of the conditions match
return 'Error - Undefined status criteria';
}
// Schedulable interface method
public void execute(SchedulableContext sc) {
// Define the batch size (optional, default is 200)
Integer batchSize = 200;
Database.executeBatch(this, batchSize);
}
}