Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit afd6e63

Browse files
authored
Feature/issue 4 (#7)
* Added sample class implementation, LeadTriggerHandler.cls * Added sample trigger implementation, Lead.trigger * Added sample test class implementation, LeadTriggerHandler_Test.cls
1 parent fedfd48 commit afd6e63

6 files changed

Lines changed: 89 additions & 0 deletions

src/classes/LeadTriggerHandler.cls

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public without sharing class LeadTriggerHandler extends TriggerHandler {
2+
3+
protected override void beforeInsert(List<SObject> newRecordList) {
4+
List<Lead> newLeadList = (List<Lead>)newRecordList;
5+
6+
for(Lead newLead : newLeadList) {
7+
this.setStatus(newLead);
8+
}
9+
}
10+
11+
protected override void beforeUpdate(List<SObject> updatedRecordList, Map<Id, SObject> updatedRecordListMap, List<SObject> oldRecordList, Map<Id, SObject> oldRecordMap) {
12+
List<Lead> updatedLeadList = (List<Lead>)updatedRecordList;
13+
Map<Id, Lead> oldLeadMap = (Map<Id, Lead>)oldRecordMap;
14+
15+
for(Lead updatedLead : updatedLeadList) {
16+
Lead oldLead = oldLeadMap.get(updatedLead.Id);
17+
18+
this.setStatus(updatedLead, oldLead);
19+
}
20+
}
21+
22+
private void setStatus(Lead updatedLead, Lead oldLead) {
23+
// Add logic here. Methods can be overloaded to handle updates & inserts
24+
if(updatedLead.Source != oldLead.Source) {
25+
this.setStatus(updatedLead);
26+
}
27+
}
28+
29+
private void setStatus(Lead lead) {
30+
// Add logic here. Methods should be simple & independent from each other (except for overloaded methods)
31+
if(lead.LeadSource == 'Web') lead.Status = 'Open - Not Contacted';
32+
}
33+
34+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>38.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@isTest
2+
private class LeadTriggerHandler_Test {
3+
4+
@isTest
5+
static void setStatus_Test() {
6+
// All methods within the handler class (LeadTriggerHandler()) are protected or private, except for the exceute(), inherited from TriggerHandler.cls
7+
// Your unit tests should test the outcome of your handler's classes methods.
8+
// This lets you refactor your handler class at any point without any major dependencies since no variables or methods are public or global
9+
// As long as your unit tests are THOROUGH, you can refactor your handler class as you see fit with confidence (as long as the tests still pass after you refactor)
10+
11+
// Create test data that should trigger (ha!) your method to run
12+
Lead lead = new Lead(
13+
Company = 'My Test Company',
14+
LastName = 'Gillespie',
15+
LeadSource = 'Web',
16+
Status = 'Closed'
17+
);
18+
// The test never calls LeadTriggerHandler - we let the Force.com platform handle executing the handler class
19+
insert lead;
20+
21+
Test.startTest();
22+
23+
// If we setup our test correctly, then the handler class's setStatus() method should have updated the test lead
24+
// Let's requery the lead so we can assert that the desired changes occurred
25+
lead = [SELECT Id, LeadSource, Status FROM Lead WHERE Id = :lead.Id];
26+
// Add asserts liberally like your life depends on it
27+
System.assertEquals('Web', lead.LeadSource);
28+
System.assertEquals('Open - Not Contacted', lead.Status);
29+
30+
Test.stopTest();
31+
}
32+
33+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>38.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

src/triggers/Lead.trigger

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
trigger Lead on Lead (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
2+
3+
// Triggers should call all before & after operations. The code within the handler class can then implement any methods needed without updating the trigger
4+
// Triggers should have no logic other than to call the corresponding handler class's execute method
5+
new LeadTriggerHandler().execute();
6+
7+
}

src/triggers/Lead.trigger-meta.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>38.0</apiVersion>
4+
<status>Active</status>
5+
</ApexTrigger>

0 commit comments

Comments
 (0)