Skip to content

Commit f0b5dea

Browse files
committed
Created test for testHandleAutoLeadScoring. Updated TestDataFactory. Fixed issue in LeadTriggerHandler
1 parent df50c24 commit f0b5dea

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

force-app/main/default/classes/LeadTriggerHandler.cls

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,18 @@ public with sharing class LeadTriggerHandler {
7676
*/
7777
public static void handleAutoLeadScoring(List<Lead> leadsToScore) {
7878
for (Lead ld : leadsToScore) {
79-
Integer score = 10;
79+
Integer score = 0;
8080

8181
// Check and add points based on the specified conditions
82-
if (ld.LeadSource == 'Website' && ld.Email != null) {
83-
score = 3;
82+
if (ld.LeadSource == 'Web' && ld.Email != null) {
83+
score += 3;
8484
}
85-
8685
if (ld.Phone != null) {
87-
score = 5;
86+
score += 5;
8887
}
89-
9088
if (ld.Industry == 'Technology') {
91-
score = 10;
89+
score += 10;
9290
}
93-
9491
ld.Lead_Score__c = score; // Set the computed score back to the lead
9592
}
9693
}

force-app/main/default/classes/LeadTriggerHandlerTest.cls

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,38 @@ private class LeadTriggerHandlerTest {
8282
Assert.areEqual('Assistant', lead.Title, 'Expected Title \'Assistant\'');
8383
}
8484
}
85+
86+
/*
87+
* LeadTriggerHandler.handleTitleNormalization update test
88+
*/
89+
@isTest
90+
static void testHandleAutoLeadScoring() {
91+
// Prepare the test data
92+
List<Map<String, Object>> params = new List<Map<String, Object>>();
93+
params.add(new Map<String, Object>{'Lead_Score__c' => 20, 'LeadSource' => null}); // 0
94+
params.add(new Map<String, Object>{'LeadSource' => 'Other', 'Email' => '[email protected]'}); // 0
95+
params.add(new Map<String, Object>{'LeadSource' => 'Web', 'Email' => '[email protected]'}); // 3
96+
params.add(new Map<String, Object>{'Phone' => '(908)345-1234', 'Industry' => 'Government'}); // 5
97+
params.add(new Map<String, Object>{'LeadSource' => 'Web', 'Email' => '[email protected]', 'Phone' => '(908)345-2345'}); // 8
98+
params.add(new Map<String, Object>{'Lead_Score__c' => 10, 'Email' => '[email protected]', 'Industry' => 'Technology'}); // 10
99+
params.add(new Map<String, Object>{'LeadSource' => 'Web', 'Email' => '[email protected]', 'Industry' => 'Technology'}); // 13
100+
params.add(new Map<String, Object>{'LeadSource' => 'Web', 'Phone' => '(908)346-1234', 'Industry' => 'Technology'}); // 15
101+
params.add(new Map<String, Object>{'LeadSource' => 'Web', 'Email' => '[email protected]',
102+
'Phone' => '(908)346-1234', 'Industry' => 'Technology'}); // 18
103+
104+
List<Lead> leadsToScore = TestDataFactory.createLeadsByParams(params, false);
105+
106+
// Perform the test
107+
Test.startTest();
108+
LeadTriggerHandler.handleAutoLeadScoring(leadsToScore);
109+
Test.stopTest();
110+
111+
// Assert that the Score calculates correctly
112+
List<Integer> scoreVariants = new List<Integer>{0, 0, 3, 5, 8, 10, 13, 15, 18};
113+
for (Integer i = 0; i < leadsToScore.size(); i++) {
114+
Assert.isTrue(leadsToScore[i].Lead_Score__c <= 18, 'Lead score shouldn\'t be more than 18');
115+
Assert.areEqual(scoreVariants[i], leadsToScore[i].Lead_Score__c, 'Score has not correctly calculated');
116+
}
117+
118+
}
85119
}

force-app/main/default/classes/TestDataFactory.cls

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,23 @@ public class TestDataFactory {
1818

1919
return leadsToInsert;
2020
}
21+
22+
public static List<Lead> createLeadsByParams(List<Map<String, Object>> params, Boolean doInsert) {
23+
List<Lead> leads = new List<Lead>();
24+
for (Integer i = 0; i < params.size(); i++) {
25+
Lead newLead = new Lead();
26+
newLead.LastName = 'Test Lead ' + i;
27+
newLead.Company = 'Test Company';
28+
Map<String, Object> locParams = params[i];
29+
for (String key : locParams.keySet()) {
30+
newLead.put(key, locParams.get(key));
31+
}
32+
leads.add(newLead);
33+
}
34+
if (doInsert) {
35+
Database.insert(leads);
36+
}
37+
38+
return leads;
39+
}
2140
}

0 commit comments

Comments
 (0)