forked from samteezy/Marketing-Holy-Grail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApex Class_ClosedLoop_Refresh v11.txt
296 lines (274 loc) · 16.9 KB
/
Apex Class_ClosedLoop_Refresh v11.txt
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
/*
* Apex Class: ClosedLoop_Refresh
* Description:
- Refresh all Closed Loop Report data
- Complete rebuild of all report data to ensure data accuracy
- Send email to current user when refresh is complete and ready for viewing
* Created By: Matthew McEachern
* Created Date: 11/7/2016
* Last Modified By: Matthew McEachern
* Last Modified: 4/25/2017
*/
global with sharing class ClosedLoop_Refresh implements Database.Batchable<sObject>{
global ClosedLoop_Refresh(){
getCLRsettings();
}
global SubC4i__Closed_Loop_Setting__c CLSsetup = null;
global String CLSinfluenceDate = null;
global Decimal CLSinfluenceStartMonths = null;
global String CLSoppAmount = null;
global String CLSreportStart = null;
global String CLSreportEnd = null;
global Set<Id> oppIds = new Set<Id>();
global Set<Id> accountIds = new Set<Id>();
global List<Date> oppCreatedDates = new List<Date>();
global Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
global Map<String,SubC4i__Closed_Loop_Report__c> stringKeyCLRmap = new Map<String,SubC4i__Closed_Loop_Report__c>();
global Map<Id,List<Opportunity>> accountOppMap = new Map<Id,List<Opportunity>>();
global Map<Id,Opportunity> updateOppsMap = new Map<Id,Opportunity>();
global List<Account> updateAccounts = new List<Account>();
global List<SubC4i__Closed_Loop_Report__c> insertCLR = new List<SubC4i__Closed_Loop_Report__c>();
global void getCLRsettings(){
if(ClosedLoop_Security.isAccessibleField('Id','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Active__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Influence_Date__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Influence_Start__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Opportunity_Amount__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Last_Data_Refresh_Completed__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Report_Start__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Report_End__c','SubC4i__Closed_Loop_Setting__c')){
List<SubC4i__Closed_Loop_Setting__c> CLSlist = new List<SubC4i__Closed_Loop_Setting__c>([SELECT Id, SubC4i__Active__c, SubC4i__Influence_Date__c, SubC4i__Influence_Start__c, SubC4i__Opportunity_Amount__c, SubC4i__Last_Data_Refresh_Completed__c, SubC4i__Report_Start__c, SubC4i__Report_End__c FROM SubC4i__Closed_Loop_Setting__c WHERE SubC4i__Active__c = true LIMIT 1]);
if(CLSlist.size() > 0){
CLSsetup = CLSlist.get(0);
CLSinfluenceDate = CLSsetup.SubC4i__Influence_Date__c;
CLSinfluenceStartMonths = CLSsetup.SubC4i__Influence_Start__c;
CLSoppAmount = CLSsetup.SubC4i__Opportunity_Amount__c;
CLSreportStart = string.valueOf(CLSsetup.SubC4i__Report_Start__c);
CLSreportEnd = string.valueOf(CLSsetup.SubC4i__Report_End__c);
}
}
}
@deprecated
global Boolean checkCreateableCLR(){return false;}
@deprecated
global Boolean checkCreateableCLRfields(){return false;}
@deprecated
global Boolean checkUpdateableCLS(){return false;}
@deprecated
global Boolean checkUpdateableCLSfields(){return false;}
global Database.QueryLocator start(Database.BatchableContext BC){
if(CLSsetup != null &&
//Account
ClosedLoop_Security.isAccessibleField('Id','Account') &&
//Contact
ClosedLoop_Security.isAccessibleField('AccountId','Contact') &&
//Opportunity
ClosedLoop_Security.isAccessibleField('Id','Opportunity') &&
ClosedLoop_Security.isAccessibleField('Name','Opportunity') &&
ClosedLoop_Security.isAccessibleField('AccountId','Opportunity') &&
ClosedLoop_Security.isAccessibleField('CreatedDate','Opportunity') &&
ClosedLoop_Security.isAccessibleField('StageName','Opportunity') &&
ClosedLoop_Security.isAccessibleField('IsClosed','Opportunity') &&
ClosedLoop_Security.isAccessibleField('IsWon','Opportunity') &&
ClosedLoop_Security.isAccessibleField('CloseDate','Opportunity') &&
ClosedLoop_Security.isAccessibleField('ForecastCategoryName','Opportunity') &&
ClosedLoop_Security.isAccessibleField('Probability','Opportunity') &&
ClosedLoop_Security.isAccessibleField(CLSoppAmount,'Opportunity') &&
//Campaign
ClosedLoop_Security.isAccessibleField('ParentId','Campaign') &&
//Campaign Member
ClosedLoop_Security.isAccessibleField('Id','CampaignMember') &&
ClosedLoop_Security.isAccessibleField('CampaignId','CampaignMember') &&
ClosedLoop_Security.isAccessibleField('CreatedDate','CampaignMember') &&
ClosedLoop_Security.isAccessibleField('ContactId','CampaignMember') &&
ClosedLoop_Security.isAccessibleField('HasResponded','CampaignMember') &&
ClosedLoop_Security.isAccessibleField('Status','CampaignMember') &&
ClosedLoop_Security.isAccessibleField(CLSinfluenceDate,'CampaignMember') &&
//Closed Loop Setting
ClosedLoop_Security.isAccessibleField('SubC4i__Active__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Influence_Date__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Influence_Start__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Opportunity_Amount__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Last_Data_Refresh_Completed__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Report_Start__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isAccessibleField('SubC4i__Report_End__c','SubC4i__Closed_Loop_Setting__c') &&
ClosedLoop_Security.isUpdateableField('SubC4i__Last_Data_Refresh_Completed__c','SubC4i__Closed_Loop_Setting__c') &&
//Closed Loop Report
ClosedLoop_Security.isCreateableField('SubC4i__Account__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Contact__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Campaign__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Parent_Campaign__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Opportunity__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Amount__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Amount__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Close_Date__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Forecast_Category__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Probability__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Stage__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Influence_Date__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Campaign_Member_Created_Date__c','SubC4i__Closed_Loop_Report__c') &&
ClosedLoop_Security.isCreateableField('SubC4i__Campaign_Member_Status__c','SubC4i__Closed_Loop_Report__c')){
string query = 'SELECT Id, Name, AccountId, CreatedDate, StageName, IsClosed, IsWon, CloseDate, ForecastCategoryName, Probability, ' + CLSoppAmount + ' FROM Opportunity WHERE (IsClosed = false OR (IsClosed = true AND IsWon = true)) AND CloseDate >= ' + CLSreportStart + ' AND CloseDate <= ' +CLSreportEnd;
return Database.getQueryLocator(query);
}
else{
system.debug('Current user does not have necessary permissions.');
System.abortJob(bc.getJobId());
return null;
}
}
global void execute(Database.BatchableContext BC, List<Opportunity> scope){
if(
//Account
!ClosedLoop_Security.isAccessibleField('Id','Account') ||
//Contact
!ClosedLoop_Security.isAccessibleField('AccountId','Contact') ||
//Opportunity
!ClosedLoop_Security.isAccessibleField('Id','Opportunity') ||
!ClosedLoop_Security.isAccessibleField('Name','Opportunity') ||
!ClosedLoop_Security.isAccessibleField('AccountId','Opportunity') ||
!ClosedLoop_Security.isAccessibleField('CreatedDate','Opportunity') ||
!ClosedLoop_Security.isAccessibleField('StageName','Opportunity') ||
!ClosedLoop_Security.isAccessibleField('IsClosed','Opportunity') ||
!ClosedLoop_Security.isAccessibleField('IsWon','Opportunity') ||
!ClosedLoop_Security.isAccessibleField('CloseDate','Opportunity') ||
!ClosedLoop_Security.isAccessibleField('ForecastCategoryName','Opportunity') ||
!ClosedLoop_Security.isAccessibleField('Probability','Opportunity') ||
!ClosedLoop_Security.isAccessibleField(CLSoppAmount,'Opportunity') ||
//Campaign
!ClosedLoop_Security.isAccessibleField('ParentId','Campaign') ||
//Campaign Member
!ClosedLoop_Security.isAccessibleField('Id','CampaignMember') ||
!ClosedLoop_Security.isAccessibleField('CampaignId','CampaignMember') ||
!ClosedLoop_Security.isAccessibleField('CreatedDate','CampaignMember') ||
!ClosedLoop_Security.isAccessibleField('ContactId','CampaignMember') ||
!ClosedLoop_Security.isAccessibleField('HasResponded','CampaignMember') ||
!ClosedLoop_Security.isAccessibleField('Status','CampaignMember') ||
!ClosedLoop_Security.isAccessibleField(CLSinfluenceDate,'CampaignMember') ||
//Closed Loop Setting
!ClosedLoop_Security.isAccessibleField('SubC4i__Active__c','SubC4i__Closed_Loop_Setting__c') ||
!ClosedLoop_Security.isAccessibleField('SubC4i__Influence_Date__c','SubC4i__Closed_Loop_Setting__c') ||
!ClosedLoop_Security.isAccessibleField('SubC4i__Influence_Start__c','SubC4i__Closed_Loop_Setting__c') ||
!ClosedLoop_Security.isAccessibleField('SubC4i__Opportunity_Amount__c','SubC4i__Closed_Loop_Setting__c') ||
!ClosedLoop_Security.isAccessibleField('SubC4i__Last_Data_Refresh_Completed__c','SubC4i__Closed_Loop_Setting__c') ||
!ClosedLoop_Security.isAccessibleField('SubC4i__Report_Start__c','SubC4i__Closed_Loop_Setting__c') ||
!ClosedLoop_Security.isAccessibleField('SubC4i__Report_End__c','SubC4i__Closed_Loop_Setting__c') ||
!ClosedLoop_Security.isUpdateableField('SubC4i__Last_Data_Refresh_Completed__c','SubC4i__Closed_Loop_Setting__c') ||
//Closed Loop Report
!ClosedLoop_Security.isCreateableField('SubC4i__Account__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Contact__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Campaign__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Parent_Campaign__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Opportunity__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Amount__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Amount__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Close_Date__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Forecast_Category__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Probability__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Opportunity_Snapshot_Stage__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Influence_Date__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Campaign_Member_Created_Date__c','SubC4i__Closed_Loop_Report__c') ||
!ClosedLoop_Security.isCreateableField('SubC4i__Campaign_Member_Status__c','SubC4i__Closed_Loop_Report__c')){
system.debug('Current user does not have necessary permissions.');
return;
}
//Copy Scope to List
oppMap.putAll(scope);
//Get Related Data
for(Opportunity o: scope){
//Get Ids
oppIds.add(o.Id);
accountIds.add(o.AccountId);
//Get Opportunity Created Dates
oppCreatedDates.add(o.CreatedDate.date());
//Sort Opportunities by Account
if(accountOppMap.containsKey(o.AccountId)){
accountOppMap.get(o.AccountId).add(o);
}
else{
List<Opportunity> tempOppList = new List<Opportunity>();
tempOppList.add(o);
accountOppMap.put(o.AccountId,tempOppList);
}
}
//Sort Opportunity Created Dates
oppCreatedDates.sort();
//Only get related Campaign Members first responded within X months before opportunity created date
Date firstOppCreatedDate = oppCreatedDates.get(0);
Integer influenceStartMonths = CLSinfluenceStartMonths.intValue();
String firstInfluenceDate = string.valueOf(firstOppCreatedDate.addMonths(-influenceStartMonths));
List<CampaignMember> campaignMemberList = new List<CampaignMember>();
string selectSOQL = 'SELECT Id, CampaignId, Campaign.ParentId, CreatedDate, ContactId, Contact.AccountId, HasResponded, Status, ' + CLSinfluenceDate;
string fromSOQL = ' FROM CampaignMember ';
string whereSOQL = 'WHERE ContactId != null AND Contact.AccountId IN: accountIds AND HasResponded = true AND ' + CLSinfluenceDate + ' != null AND ' + CLSinfluenceDate + ' >= ' + firstInfluenceDate + ' AND ' + CLSinfluenceDate + ' <= ' + CLSreportEnd;
string completeSOQL = selectSOQL + fromSOQL + whereSOQL;
campaignMemberList = Database.query(completeSOQL);
//Create CLR records for Opportunities WITH Campaign Influence
for(CampaignMember cm: campaignMemberList){
if(accountOppMap.containsKey(cm.Contact.AccountId)){
for(Opportunity o: accountOppMap.get(cm.Contact.AccountId)){
if((Date)cm.get(CLSinfluenceDate) >= o.CreatedDate.addMonths(-CLSinfluenceStartMonths.intValue()) && ((o.IsClosed == false) || (o.IsClosed == true && (Date)cm.get(CLSinfluenceDate) <= o.CloseDate))){
//Unique Key to prevent duplicates
String uniqueKey = string.valueOf(o.AccountId) + string.valueOf(cm.CampaignId) + string.valueOf(cm.ContactId) + string.valueOf(o.Id);
if(!stringKeyCLRmap.containsKey(uniqueKey)){
//Remove Opportunities w/campaign influence from oppMap
oppMap.remove(o.Id);
//Create new Closed Loop Report records
SubC4i__Closed_Loop_Report__c CLR = new SubC4i__Closed_Loop_Report__c(
SubC4i__Account__c = o.AccountId,
SubC4i__Campaign__c = cm.CampaignId,
SubC4i__Parent_Campaign__c = cm.Campaign.ParentId,
SubC4i__Contact__c = cm.ContactId,
SubC4i__Opportunity__c = o.Id,
SubC4i__Opportunity_Snapshot_Amount__c = (Decimal)o.get(CLSoppAmount),
SubC4i__Opportunity_Snapshot_Close_Date__c = o.CloseDate,
SubC4i__Opportunity_Snapshot_Forecast_Category__c = o.ForecastCategoryName,
SubC4i__Opportunity_Snapshot_Probability__c = o.Probability,
SubC4i__Opportunity_Snapshot_Stage__c = o.StageName,
SubC4i__Influence_Date__c = (Date)cm.get(CLSinfluenceDate),
SubC4i__Campaign_Member_Created_Date__c = cm.CreatedDate,
SubC4i__Campaign_Member_Status__c = cm.Status
);
stringKeyCLRmap.put(uniqueKey,CLR);
insertCLR.add(CLR);
}
}
}
}
}
//Create CLR records for Opportunities WITHOUT Campaign Influence
for(Opportunity o: oppMap.values()){
SubC4i__Closed_Loop_Report__c CLR = new SubC4i__Closed_Loop_Report__c(
SubC4i__Account__c = o.AccountId,
SubC4i__Opportunity__c = o.Id,
SubC4i__Opportunity_Amount__c = (Decimal)oppMap.get(o.Id).get(CLSoppAmount)
);
insertCLR.add(CLR);
}
if(insertCLR.size() > 0){
insert insertCLR;
}
if(oppIds.size() > 0){
SubC4i.ClosedLoop_Metrics updateMetrics = new SubC4i.ClosedLoop_Metrics(accountIds, oppIds);
Id jobId = System.enqueueJob(updateMetrics);
}
}
global void finish(Database.BatchableContext BC){
//Send Email Notification
String subjectLine = 'Salesforce Alert - Marketing Holy Grail data refresh has completed';
String messageBodyText =
'The closed loop data refresh for the Marketing Holy Grail has completed.';
Messaging.reserveSingleEmailCapacity(1);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(userinfo.getUserId());
mail.setSaveAsActivity(false);
mail.setSubject(subjectLine);
mail.setPlainTextBody(messageBodyText);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
//Update Last Data Refresh Completed timestamp
if(ClosedLoop_Security.isUpdateableField('SubC4i__Last_Data_Refresh_Completed__c','SubC4i__Closed_Loop_Setting__c')){
CLSsetup.SubC4i__Last_Data_Refresh_Completed__c = datetime.now();
update CLSsetup;
}
}
}