From 2b6fcd65f038b8f0afcb4e8e91a78bff207e4290 Mon Sep 17 00:00:00 2001 From: Matthew Yates Date: Mon, 2 Nov 2026 13:56:01 -0500 Subject: [PATCH] feat(credit): account health score for sales reps --- .../classes/AccountHealthCalculator.cls | 27 ++++++++++++ .../AccountHealthCalculator.cls-meta.xml | 5 +++ .../classes/AccountHealthController.cls | 42 +++++++++++++++++++ .../AccountHealthController.cls-meta.xml | 5 +++ .../classes/AccountHealthControllerTest.cls | 24 +++++++++++ .../AccountHealthControllerTest.cls-meta.xml | 5 +++ .../fields/Health_Score__c.field-meta.xml | 11 +++++ .../triggers/AccountHealthTrigger.trigger | 13 ++++++ .../AccountHealthTrigger.trigger-meta.xml | 5 +++ 9 files changed, 137 insertions(+) create mode 100644 force-app/main/default/classes/AccountHealthCalculator.cls create mode 100644 force-app/main/default/classes/AccountHealthCalculator.cls-meta.xml create mode 100644 force-app/main/default/classes/AccountHealthController.cls create mode 100644 force-app/main/default/classes/AccountHealthController.cls-meta.xml create mode 100644 force-app/main/default/classes/AccountHealthControllerTest.cls create mode 100644 force-app/main/default/classes/AccountHealthControllerTest.cls-meta.xml create mode 100644 force-app/main/default/objects/Account/fields/Health_Score__c.field-meta.xml create mode 100644 force-app/main/default/triggers/AccountHealthTrigger.trigger create mode 100644 force-app/main/default/triggers/AccountHealthTrigger.trigger-meta.xml diff --git a/force-app/main/default/classes/AccountHealthCalculator.cls b/force-app/main/default/classes/AccountHealthCalculator.cls new file mode 100644 index 0000000..00aa7ce --- /dev/null +++ b/force-app/main/default/classes/AccountHealthCalculator.cls @@ -0,0 +1,27 @@ +public class AccountHealthCalculator { + public static Decimal score(Account acc, Integer failedOrders) { + Decimal s = 100; + if (acc.Credit_Limit__c != null && acc.Credit_Limit__c > 0) { + Decimal util = acc.Open_Order_Value__c / acc.Credit_Limit__c; + if (util > 0.9) { + s = s - 40; + } else if (util > 0.75) { + s = s - 25; + } else if (util > 0.5) { + s = s - 10; + } + } + if (acc.Credit_Hold__c) { + s = s - 30; + } + s = s - (failedOrders * 5); + s = acc.Customer_Tier__c == 'Platinum' ? s + 10 : acc.Customer_Tier__c == 'Gold' ? s + 5 : s; + if (s < 0) { + s = 0; + } + if (s > 100) { + s = 100; + } + return s; + } +} diff --git a/force-app/main/default/classes/AccountHealthCalculator.cls-meta.xml b/force-app/main/default/classes/AccountHealthCalculator.cls-meta.xml new file mode 100644 index 0000000..82775b9 --- /dev/null +++ b/force-app/main/default/classes/AccountHealthCalculator.cls-meta.xml @@ -0,0 +1,5 @@ + + + 65.0 + Active + diff --git a/force-app/main/default/classes/AccountHealthController.cls b/force-app/main/default/classes/AccountHealthController.cls new file mode 100644 index 0000000..e510071 --- /dev/null +++ b/force-app/main/default/classes/AccountHealthController.cls @@ -0,0 +1,42 @@ +public class AccountHealthController { + @AuraEnabled + public static List searchAccounts(String searchTerm, String sortField) { + String query = + 'SELECT Id, Name, Health_Score__c, Customer_Tier__c, Credit_Limit__c, Open_Order_Value__c ' + + 'FROM Account WHERE Name LIKE \'%' + + searchTerm + + '%\' ORDER BY ' + + sortField; + return Database.query(query); + } + + @AuraEnabled(cacheable=true) + public static List> getPortfolioHealth() { + List> results = new List>(); + List accounts = [SELECT Id, Name, Health_Score__c, Customer_Tier__c FROM Account]; + List orders = [SELECT Id, AccountId, TotalAmount, Fulfillment_Status__c FROM Order]; + for (Account acc : accounts) { + Decimal openTotal = 0; + Integer failed = 0; + for (Order o : orders) { + if (o.AccountId == acc.Id) { + openTotal += o.TotalAmount; + if (o.Fulfillment_Status__c == 'Failed') { + failed++; + } + } + } + results.add( + new Map{ + 'id' => acc.Id, + 'name' => acc.Name, + 'score' => acc.Health_Score__c, + 'tier' => acc.Customer_Tier__c, + 'openTotal' => openTotal, + 'failedOrders' => failed + } + ); + } + return results; + } +} diff --git a/force-app/main/default/classes/AccountHealthController.cls-meta.xml b/force-app/main/default/classes/AccountHealthController.cls-meta.xml new file mode 100644 index 0000000..82775b9 --- /dev/null +++ b/force-app/main/default/classes/AccountHealthController.cls-meta.xml @@ -0,0 +1,5 @@ + + + 65.0 + Active + diff --git a/force-app/main/default/classes/AccountHealthControllerTest.cls b/force-app/main/default/classes/AccountHealthControllerTest.cls new file mode 100644 index 0000000..d7830ff --- /dev/null +++ b/force-app/main/default/classes/AccountHealthControllerTest.cls @@ -0,0 +1,24 @@ +@IsTest(SeeAllData=true) +private class AccountHealthControllerTest { + @IsTest + static void testSearch() { + List results = AccountHealthController.searchAccounts('a', 'Name'); + Assert.isTrue(true); + } + + @IsTest + static void testPortfolio() { + AccountHealthController.getPortfolioHealth(); + } + + @IsTest + static void testScore() { + Account acc = [ + SELECT Id, Credit_Limit__c, Open_Order_Value__c, Credit_Hold__c, Customer_Tier__c + FROM Account + LIMIT 1 + ]; + Decimal s = AccountHealthCalculator.score(acc, 0); + System.assert(s != null); + } +} diff --git a/force-app/main/default/classes/AccountHealthControllerTest.cls-meta.xml b/force-app/main/default/classes/AccountHealthControllerTest.cls-meta.xml new file mode 100644 index 0000000..82775b9 --- /dev/null +++ b/force-app/main/default/classes/AccountHealthControllerTest.cls-meta.xml @@ -0,0 +1,5 @@ + + + 65.0 + Active + diff --git a/force-app/main/default/objects/Account/fields/Health_Score__c.field-meta.xml b/force-app/main/default/objects/Account/fields/Health_Score__c.field-meta.xml new file mode 100644 index 0000000..3a76a6b --- /dev/null +++ b/force-app/main/default/objects/Account/fields/Health_Score__c.field-meta.xml @@ -0,0 +1,11 @@ + + + Health_Score__c + false + + 5 + false + 0 + false + Number + diff --git a/force-app/main/default/triggers/AccountHealthTrigger.trigger b/force-app/main/default/triggers/AccountHealthTrigger.trigger new file mode 100644 index 0000000..7cc38be --- /dev/null +++ b/force-app/main/default/triggers/AccountHealthTrigger.trigger @@ -0,0 +1,13 @@ +trigger AccountHealthTrigger on Account(before update) { + for (Account acc : Trigger.new) { + Account oldAcc = Trigger.oldMap.get(acc.Id); + if (acc.Open_Order_Value__c != oldAcc.Open_Order_Value__c || acc.Credit_Limit__c != oldAcc.Credit_Limit__c) { + Integer failedOrders = [ + SELECT COUNT() + FROM Order + WHERE AccountId = :acc.Id AND Fulfillment_Status__c = 'Failed' + ]; + acc.Health_Score__c = AccountHealthCalculator.score(acc, failedOrders); + } + } +} diff --git a/force-app/main/default/triggers/AccountHealthTrigger.trigger-meta.xml b/force-app/main/default/triggers/AccountHealthTrigger.trigger-meta.xml new file mode 100644 index 0000000..870203f --- /dev/null +++ b/force-app/main/default/triggers/AccountHealthTrigger.trigger-meta.xml @@ -0,0 +1,5 @@ + + + 65.0 + Active +