Created
May 17, 2024 21:36
-
-
Save danicacodes/a5e8ab43b7731967d619931a9ec0f88c to your computer and use it in GitHub Desktop.
Danica Weber - RAD Final Project
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); | |
} | |
} | |
// A Utility class to process Stock Item records from the Stock Item Handler | |
public with sharing class StockItemHandler { | |
// Constructor for the StockItemHandler class | |
public StockItemHandler() { | |
} | |
// Method to be called before inserting new Stock_Item__c records | |
public static void OnBeforeInsert(List<Stock_Item__c> newStockItems) { | |
// Map to hold existing stock items with their names as keys | |
Map<String, Stock_Item__c> stockItemMap = new Map<String, Stock_Item__c>(); | |
// Set to collect unique item names from new stock items | |
Set<String> stockItemSet = new Set<String>(); | |
// Loop through new stock items to add their names to the set | |
for (Stock_Item__c si : newStockItems) { | |
stockItemSet.add(si.Item_Name__c); | |
System.debug(si.Item_Name__c); // Debug log to print the item name | |
} | |
// Query existing stock items that have the same names as new stock items | |
for (Stock_Item__c s : [SELECT ID, Item_Name__c FROM Stock_Item__c WHERE Item_Name__c IN :stockItemSet LIMIT 10]) { | |
stockItemMap.put(s.Item_Name__c, s); // Add existing items to the map | |
} | |
// Loop through new stock items to check for duplicates | |
for (Stock_Item__c dupItems : newStockItems) { | |
if (stockItemMap.containsKey(dupItems.Item_Name__c)) { | |
// Append ' - Duplicate Item' to the name of duplicate items | |
dupItems.Item_Name__c = dupItems.Item_Name__c + ' * Duplicate Item'; | |
} | |
} | |
} | |
// Method to be called before deleting Stock_Item__c records | |
public static void onBeforeDelete(List<Stock_Item__c> deletedStockItems) { | |
List<Case> insertCase = new List<Case>(); // List to hold cases to be inserted | |
String stockOnHand; // Variable to hold stock on hand as a string | |
// Loop through items to be deleted | |
for (Stock_Item__c deletedItem : deletedStockItems) { | |
if (deletedItem.Stock_on_Hand__c != 0) { | |
// Convert stock on hand to a string | |
stockOnHand = deletedItem.Stock_on_Hand__c.toPlainString(); | |
// Create a new case for each item with stock on hand | |
Case c = new Case(); | |
c.Status = 'New'; | |
c.Origin = 'Deleted Stock Item'; | |
c.Subject = 'Delete Stock Item: ' + deletedItem.Item_Name__c; | |
c.Description = 'Delete Stock ID: ' + deletedItem.Id + | |
' Item Name: ' + deletedItem.Item_Name__c + | |
' Stock at hand when deleted: ' + deletedItem.Stock_on_Hand__c; | |
insertCase.add(c); // Add the case to the list | |
} | |
} | |
// Insert cases if there are any in the list | |
if (insertCase.size() > 0) { | |
Insert insertCase; | |
} | |
} | |
// Method to get stock items with low stock levels | |
public static List<Stock_Item__c> getLowStockItems() { | |
// Query stock items where the stock is low | |
List<Stock_Item__c> lowItems = [SELECT ID, Item_Name__c, Item_Stock_is_Low__c, Minimum_Stock_Level__c, Stock_on_Hand__c | |
FROM Stock_Item__c | |
WHERE Item_Stock_is_Low__c = TRUE]; | |
// Return the list so it can be used in other processes | |
return lowItems; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment