-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeadStatusBatchTest
More file actions
233 lines (197 loc) · 9.47 KB
/
Copy pathLeadStatusBatchTest
File metadata and controls
233 lines (197 loc) · 9.47 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
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
@isTest
public class LeadStatusBatchTest {
// Helper method to convert a Lead with optional existing Account, Contact, Opportunity, and control over Opportunity creation
private static void convertLeadHelper(Lead lead, Id existingAccountId, Id existingContactId, Id existingOpportunityId, Boolean doNotCreateOpp) {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);
lc.setConvertedStatus('Converted'); // Ensure this status exists in your org
if (existingAccountId != null) {
lc.setAccountId(existingAccountId);
}
if (existingContactId != null) {
lc.setContactId(existingContactId);
}
if (existingOpportunityId != null) {
lc.setOpportunityId(existingOpportunityId);
lc.setDoNotCreateOpportunity(false); // Associate with existing Opportunity
} else {
lc.setDoNotCreateOpportunity(doNotCreateOpp);
}
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess(), 'Lead conversion failed for Lead ID: ' + lead.Id);
}
@testSetup
static void setupTestData() {
// Create an existing Account with BillingCountryCode and set CreatedDate in the past
Account existingAccount = new Account(
Name = 'Existing Account',
BillingCountryCode = 'US'
);
insert existingAccount;
// Set Account CreatedDate to 30 days in the past
Test.setCreatedDate(existingAccount.Id, System.now().addDays(-30));
// Create an existing Contact associated with the existing Account
Contact existingContact = new Contact(
FirstName = 'Existing',
LastName = 'Contact',
AccountId = existingAccount.Id
);
insert existingContact;
// Set Contact CreatedDate to 30 days in the past
Test.setCreatedDate(existingContact.Id, System.now().addDays(-30));
// Create an existing Opportunity associated with the existing Account
Opportunity existingOpportunity = new Opportunity(
Name = 'Existing Opportunity',
StageName = 'Analyzing & Qualifying Needs',
CloseDate = Date.today().addDays(30),
AccountId = existingAccount.Id
);
insert existingOpportunity;
// Set Opportunity CreatedDate to 30 days in the past
Test.setCreatedDate(existingOpportunity.Id, System.now().addDays(-30));
// Create Leads to be converted
// Lead 1: Convert to new Account, new Contact, no Opportunity
Lead lead1 = new Lead(
LastName = 'Lead1',
Company = 'Company1',
Status = 'Open',
Email = 'lead1@newaccnewconnoopp.com',
CountryCode = 'US'
);
// Lead 2: Convert to new Account, new Contact, new Opportunity
Lead lead2 = new Lead(
LastName = 'Lead2',
Company = 'Company2',
Status = 'Open',
Email = 'lead2@newaccnewconnewopp.com',
CountryCode = 'US'
);
// Lead 3: Convert to existing Account, new Contact, no Opportunity
Lead lead3 = new Lead(
LastName = 'Lead3',
Company = 'Company3',
Status = 'Open',
Email = 'lead3@exaccnewconnoopp.com',
CountryCode = 'US'
);
// Lead 4: Convert to existing Account, new Contact, existing Opportunity
Lead lead4 = new Lead(
LastName = 'Lead4',
Company = 'Company4',
Status = 'Open',
Email = 'lead4@exaccnewconexopp.com',
CountryCode = 'US'
);
// Lead 5: Convert to existing Account, new Contact, new Opportunity
Lead lead5 = new Lead(
LastName = 'Lead5',
Company = 'Company5',
Status = 'Open',
Email = 'lead5@exaccnewconnewopp.com',
CountryCode = 'US'
);
// Lead 6: Convert to existing Account, existing Contact, no Opportunity
Lead lead6 = new Lead(
LastName = 'Lead6',
Company = 'Company6',
Status = 'Open',
Email = 'lead6@exaccexconnoopp.com',
CountryCode = 'US'
);
// Lead 7: Convert to existing Account, existing Contact, existing Opportunity
Lead lead7 = new Lead(
LastName = 'Lead7',
Company = 'Company7',
Status = 'Open',
Email = 'lead7@exaccexconexopp.com',
CountryCode = 'US'
);
// Lead 8: Convert to existing Account, existing Contact, new Opportunity
Lead lead8 = new Lead(
LastName = 'Lead8',
Company = 'Company8',
Status = 'Open',
Email = 'lead8@exaccexconnewopp.com',
CountryCode = 'US'
);
insert new List<Lead>{ lead1, lead2, lead3, lead4, lead5, lead6, lead7, lead8 };
// Convert Leads
// Lead1: New Account & Contact, No Opportunity/doNotCreateOpp
convertLeadHelper(lead1, null, null, null, true);
// Lead2: New Account & Contact, New Opportunity
convertLeadHelper(lead2, null, null, null, false);
// Lead3: Existing Account, New Contact, No Opportunity/doNotCreateOpp
convertLeadHelper(lead3, existingAccount.Id, null, null, true);
// Lead4: Existing Account, New Contact, Existing Opportunity
convertLeadHelper(lead4, existingAccount.Id, null, existingOpportunity.Id, false);
// Lead5: Existing Account, New Contact, New Opportunity
convertLeadHelper(lead5, existingAccount.Id, null, null, false);
// Lead6: Existing Account, Existing Contact, No Opportunity/doNotCreateOpp
convertLeadHelper(lead6, existingAccount.Id, existingContact.Id, null, true);
// Lead7: Existing Account, Existing Contact, Existing Opportunity
convertLeadHelper(lead7, existingAccount.Id, existingContact.Id, existingOpportunity.Id, false);
// Lead8: Existing Account, Existing Contact, New Opportunity
convertLeadHelper(lead8, existingAccount.Id, existingContact.Id, null, false);
}
@isTest
static void testBatchExecution() {
// Start test context
Test.startTest();
// Instantiate the batch class
LeadStatusBatch batch = new LeadStatusBatch();
// Execute the batch
Database.executeBatch(batch, 200);
// End test context
Test.stopTest();
// Verify that Leads have been updated correctly
// Query converted Leads to check their new Status
List<Lead> convertedLeads = [
SELECT Id, LastName, Status, Email, ConvertedDate, ConvertedAccountId, ConvertedAccount.CreatedDate, ConvertedContactId, ConvertedContact.CreatedDate, ConvertedOpportunityId, ConvertedOpportunity.CreatedDate
FROM Lead
WHERE IsConverted = true
];
// Expected statuses based on determineNewStatus logic:
// Lead1: 'Converted - New Acc, New Con, No Opp'
// Lead2: 'Converted - New Acc, New Con, New Opp'
// Lead3: 'Converted - Ex Acc, New Con, No Opp'
// Lead4: 'Converted - Ex Acc, New Con, Ex Opp'
// Lead5: 'Converted - Ex Acc, New Con, New Opp'
// Lead6: 'Converted - Ex Acc, Ex Con, No Opp'
// Lead7: 'Converted - Ex Acc, Ex Con, Ex Opp'
// Lead8: 'Converted - Ex Acc, Ex Con, New Opp'
for (Lead ld : convertedLeads) {
switch on ld.Email {
when 'lead1@newaccnewconnoopp.com' {
System.assertEquals('Converted - New Acc, New Con, No Opp', ld.Status, 'Lead1 status mismatch');
}
when 'lead2@newaccnewconnewopp.com' {
System.assertEquals('Converted - New Acc, New Con, New Opp', ld.Status, 'Lead2 status mismatch');
}
when 'lead3@exaccnewconnoopp.com' {
System.assertEquals('Converted - Ex Acc, New Con, No Opp', ld.Status, 'Lead3 status mismatch');
}
when 'lead4@exaccnewconexopp.com' {
System.assertEquals('Converted - Ex Acc, New Con, Ex Opp', ld.Status, 'Lead4 status mismatch');
}
when 'lead5@exaccnewconnewopp.com' {
System.assertEquals('Converted - Ex Acc, New Con, New Opp', ld.Status, 'Lead5 status mismatch');
}
when 'lead6@exaccexconnoopp.com' {
System.assertEquals('Converted - Ex Acc, Ex Con, No Opp', ld.Status, 'Lead6 status mismatch');
}
when 'lead7@exaccexconexopp.com' {
System.assertEquals('Converted - Ex Acc, Ex Con, Ex Opp', ld.Status, 'Lead7 status mismatch');
}
when 'lead8@exaccexconnewopp.com' {
System.assertEquals('Converted - Ex Acc, Ex Con, New Opp', ld.Status, 'Lead8 status mismatch');
}
when else {
System.assert(false, 'Unexpected Lead found: ' + ld.LastName + ' ' + ld.Email + ' ' + ld.Status);
}
}
}
// Verify that no ErrorLog__c records were created for successful updates
// List<ErrorLog__c> errorLogs = [SELECT Id FROM ErrorLog__c];
// System.assertEquals(0, errorLogs.size(), 'Unexpected error logs were created.');
}
}