Created
May 17, 2024 13:26
-
-
Save danicacodes/96fa46a6109a3d0704dd153a6b05489d to your computer and use it in GitHub Desktop.
Danica Weber Final Project (Draft w Unresolved Errors)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger StockItemTrigger on Stock_Item__c (before insert, before delete) { | |
// Before Insert Handling | |
if (Trigger.isInsert && Trigger.isBefore) { | |
// Call the class in your handler for before insert | |
StockItemHandler.onBeforeInsert(Trigger.new); | |
} | |
// Before Delete Handling | |
if (Trigger.isDelete && Trigger.isBefore) { | |
// Call the class in your handler for before delete | |
StockItemHandler.onBeforeDelete(Trigger.old); | |
} | |
} | |
public class StockItemHandler_Test { | |
static void setup() { | |
// Common setup code if needed for multiple test methods | |
} | |
static void testOnBeforeInsert_NoDuplicateName() { | |
// Test inserting a Stock_Item__c without a duplicate name | |
Stock_Item__c stockItem = new Stock_Item__c(Name = 'Unique Item', Stock_On_Hand__c = 5, Minimum_Stock_Level__c = 10); | |
Test.startTest(); | |
insert stockItem; | |
Test.stopTest(); | |
// Verify that the stock item was inserted correctly | |
Stock_Item__c insertedItem = [SELECT Id, Name FROM Stock_Item__c WHERE Name = 'Unique Item' LIMIT 1]; | |
System.assertEquals('Unique Item', insertedItem.Name); | |
} | |
static void testOnBeforeInsert_WithDuplicateName() { | |
// Create an existing stock item with a specific name | |
Stock_Item__c existingItem = new Stock_Item__c(Name = 'Duplicate Item', Stock_On_Hand__c = 5, Minimum_Stock_Level__c = 10); | |
insert existingItem; | |
// Attempt to insert a new stock item with the same name | |
Stock_Item__c newItem = new Stock_Item__c(Name = 'Duplicate Item', Stock_On_Hand__c = 5, Minimum_Stock_Level__c = 10); | |
Test.startTest(); | |
insert newItem; | |
Test.stopTest(); | |
// Verify that the new stock item's name was modified | |
Stock_Item__c insertedNewItem = [SELECT Id, Name FROM Stock_Item__c WHERE Id = :newItem.Id]; | |
System.assertEquals('Duplicate Item Duplicate Item', insertedNewItem.Name); | |
} | |
static void testOnBeforeDelete_StockOnHandNotZero() { | |
// Create a stock item with non-zero stock on hand | |
Stock_Item__c stockItem = new Stock_Item__c(Name = 'Item to Delete', Stock_On_Hand__c = 5, Minimum_Stock_Level__c = 10); | |
insert stockItem; | |
// Attempt to delete the stock item | |
Test.startTest(); | |
try { | |
delete stockItem; | |
} catch (DmlException e) { | |
// Expected exception due to the addError call in the trigger | |
} | |
Test.stopTest(); | |
// Verify that the stock item was not deleted | |
Stock_Item__c undeletedItem = [SELECT Id, Name FROM Stock_Item__c WHERE Id = :stockItem.Id]; | |
System.assertEquals('Item to Delete', undeletedItem.Name); | |
// Verify that a case was created | |
Case createdCase = [SELECT Id, Description FROM Case WHERE Description LIKE '%Item to Delete%' LIMIT 1]; | |
System.assert(createdCase != null); | |
System.assert(createdCase.Description.contains('Item to Delete')); | |
} | |
static void testGetLowStockItems() { | |
// Create stock items with varying stock levels | |
Stock_Item__c lowStockItem1 = new Stock_Item__c(Name = 'Low Stock Item 1', Stock_On_Hand__c = 3, Minimum_Stock_Level__c = 5); | |
Stock_Item__c lowStockItem2 = new Stock_Item__c(Name = 'Low Stock Item 2', Stock_On_Hand__c = 1, Minimum_Stock_Level__c = 2); | |
Stock_Item__c sufficientStockItem = new Stock_Item__c(Name = 'Sufficient Stock Item', Stock_On_Hand__c = 10, Minimum_Stock_Level__c = 5); | |
insert new List<Stock_Item__c> { lowStockItem1, lowStockItem2, sufficientStockItem }; | |
// Query for low stock items using the method | |
Test.startTest(); | |
List<Stock_Item__c> lowStockItems = StockItemHandler.getLowStockItems(); | |
Test.stopTest(); | |
// Verify that the correct low stock items are returned | |
System.assertEquals(2, lowStockItems.size()); | |
Set<String> lowStockItemNames = new Set<String>(); | |
for (Stock_Item__c item : lowStockItems) { | |
lowStockItemNames.add(item.Name); | |
} | |
System.assert(lowStockItemNames.contains('Low Stock Item 1')); | |
System.assert(lowStockItemNames.contains('Low Stock Item 2')); | |
} | |
} | |
public class StockItemHandler { | |
public static void onBeforeInsert(List<Stock_Item__c> newStockItems) { | |
// Collect the names of the new stock items | |
Set<String> newNames = new Set<String>(); | |
for (Stock_Item__c stockItem : newStockItems) { | |
newNames.add(stockItem.Name); | |
} | |
// Query existing stock items with the same names | |
Map<String, Stock_Item__c> existingStockItems = new Map<String, Stock_Item__c>( | |
[SELECT Id, Name FROM Stock_Item__c WHERE Name IN :newNames] | |
); | |
// Iterate through new stock items and check for duplicates | |
for (Stock_Item__c stockItem : newStockItems) { | |
if (existingStockItems.containsKey(stockItem.Name)) { | |
// Modify the name to include "Duplicate Item" | |
stockItem.Name += ' Duplicate Item'; | |
} | |
} | |
} | |
public static void onBeforeDelete(List<Stock_Item__c> oldStockItems) { | |
List<Case> casesToCreate = new List<Case>(); | |
for (Stock_Item__c stockItem : oldStockItems) { | |
// Check if stock on hand is not zero | |
if (stockItem.Stock_On_Hand__c != 0) { | |
// Create a case to alert someone | |
Case newCase = new Case( | |
Subject = 'Stock Item Deletion Alert', | |
Status = 'New', | |
Priority = 'High', | |
Description = 'Stock item "' + stockItem.Name + '" (ID: ' + stockItem.Id + ') is being deleted with ' + stockItem.Stock_On_Hand__c + ' units on hand.' | |
); | |
casesToCreate.add(newCase); | |
// Prevent deletion by adding an error | |
stockItem.addError('Stock on hand must be 0 before deletion.'); | |
} | |
} | |
// Insert the cases outside of the loop to avoid DML limits | |
if (!casesToCreate.isEmpty()) { | |
insert casesToCreate; | |
} | |
} | |
public static List<Stock_Item__c> getLowStockItems() { | |
// Query to get stock items with stock on hand at or below their minimum stock level | |
List<Stock_Item__c> lowStockItems = [ | |
SELECT Id, Item_Name__c, Item_Stock_is_Low__c, Minimum_Stock_Level__c, Stock_on_Hand__c | |
FROM Stock_Item__c | |
WHERE Stock_on_Hand__c <= Minimum_Stock_Level__c | |
]; | |
return lowStockItems; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment