Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add trigger and test framework #32

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .forceignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ package.xml

**/appMenus/**
**/profiles/**
**/listviews/**
**/reports/**
**/dashboards/**
**/reportTypes/**
15 changes: 15 additions & 0 deletions force-app/main/default/classes/AccountHelper.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public with sharing class AccountHelper {
public static void setSLA(List<Account> accList) {
for(Account acc: accList) {
if(acc.AnnualRevenue >= 1000000) {
acc.SLA__c = 'Platinum';
} else if (acc.AnnualRevenue >= 100000) {
acc.SLA__c = 'Gold';
} else if (acc.AnnualRevenue >= 10000) {
acc.SLA__c = 'Silver';
} else {
acc.SLA__c = 'Bronze';
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>
</ApexClass>
22 changes: 22 additions & 0 deletions force-app/main/default/classes/AccountHelper_Test.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@isTest(SeeAllData=false)
private with sharing class AccountHelper_Test {

@isTest
private static void testSetSLA() {
List<Account> accs = TestDataFactory.createTestAccounts(4, false);
accs[0].AnnualRevenue = 1;
accs[1].AnnualRevenue = 10000;
accs[2].AnnualRevenue = 100000;
accs[3].AnnualRevenue = 1000000;

Test.startTest();
insert accs;
Test.stopTest();

List<Account> accResult = [SELECT Id, SLA__c, Name FROM Account ORDER BY AnnualRevenue ASC];
Assert.areEqual('Bronze', accResult[0].SLA__c, 'SLA was not set correctly.');
Assert.areEqual('Silver', accResult[1].SLA__c, 'SLA was not set correctly.');
Assert.areEqual('Gold', accResult[2].SLA__c, 'SLA was not set correctly.');
Assert.areEqual('Platinum', accResult[3].SLA__c, 'SLA was not set correctly.');
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion>
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>
</ApexClass>
29 changes: 29 additions & 0 deletions force-app/main/default/classes/AccountTriggerHandler.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public with sharing class AccountTriggerHandler extends TriggerHandler {
public override Boolean isDisabled() {
return BypassAutomation__c.getInstance().Accounts__c;
}

public override void applyDefaults() {
for(Account acc: (List<Account>) Trigger.New) {
if(String.isBlank(acc.Description)) {
acc.Description = 'New Account';
}
}
}

public override void validate() {
for(Account acc: (List<Account>) Trigger.New) {
if(acc.BillingStreet == null) {
acc.BillingStreet.addError('Address is required for new accounts.');
}
}
}

public override void beforeInsert() {
AccountHelper.setSLA((List<Account>) Trigger.New);
}

public override void beforeUpdate() {
AccountHelper.setSLA((List<Account>) Trigger.New);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>
5 changes: 0 additions & 5 deletions force-app/main/default/classes/AccountTriggerHelper.cls

This file was deleted.

7 changes: 0 additions & 7 deletions force-app/main/default/classes/AccountTriggerTest.cls

This file was deleted.

14 changes: 14 additions & 0 deletions force-app/main/default/classes/TestDataFactory.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@isTest(SeeAllData=False)
public with sharing class TestDataFactory {
public static List<Account> createTestAccounts(Integer num, boolean doInsert) {
List<Account> accs = new List<Account>();
for(Integer i=0;i < num; i++) {
accs.add(new Account(
Name = 'Test Account ' + i,
BillingStreet = '123 Test Street'
));
}
if(doInsert) { insert accs; }
return accs;
}
}
5 changes: 5 additions & 0 deletions force-app/main/default/classes/TestDataFactory.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading