Last active
May 22, 2025 12:28
-
-
Save abbiebauer37/4d1315ede5f2f6f057212419f64b16bd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public with sharing class StockItemHandler { | |
//We are declaring a method to be referenced in the StockItemTrigger called duplicateItem that pulls in a list of new Stock Items; | |
public static void duplicateItems (List <Stock_Item__c> newStockItems){ | |
//We are declaring a set of Item Names | |
Set<String> itemNames = new Set <String>(); | |
//We are running a for loop that runs through our new Stock items; | |
for (Stock_Item__c item : newStockItems){ | |
if (item.Item_Name__c != null){ | |
//if the item name is not null we are adding the item name to the itemNames set; | |
itemNames.add(item.Item_Name__c); | |
} | |
} | |
//We are declaring a map that includes a string for the item name and stock item record; | |
Map <String, Stock_Item__c> existingItemsMap = new Map <String, Stock_Item__c>(); | |
//We are running a SOQL for loop to select the Stock Item name from the Stock Item object where the name is in the itemNames set we declared earlier; | |
for (Stock_Item__c existing: [SELECT Item_Name__c FROM Stock_Item__c WHERE Item_Name__c IN :itemNames]){ | |
//If the Stock Item Name already exists we are putting the name and record into our existingItemsMap; | |
existingItemsMap.put(existing.Item_Name__c, existing); | |
} | |
//We are running a for loop to cycle through the new items; | |
for (Stock_Item__c newItem : newStockItems) { | |
//We are checking to see if the new item's name matches the key from our existing map; | |
if (existingItemsMap.containsKey(newItem.Item_Name__c)) { | |
//If it does then we are appending ' - Duplicate Item' to the end of the item name; | |
newItem.Item_Name__c += ' - Duplicate Item'; | |
} | |
} | |
} | |
//We are declaring a method to be referenced in StockItemTrigger called checkStockAmountBeforeDelete that pulls in a list of stock items that may be deleted; | |
public static void checkStockAmountBeforeDelete (List <Stock_Item__c> itemsToBeDeleted){ | |
//We are declaring a list of cases to insert; | |
List <Case> casesToInsert = new List <Case>(); | |
//itemsToBeDeleted = [SELECT id, Stock_on_Hand__c FROM Stock_Item__c]; | |
//We are running a for loop to cycle through our stock items list; | |
for (Stock_Item__c stockItem :itemsToBeDeleted){ | |
//If the stock on hand is not zero, then we are creating a new case; | |
if (stockItem.Stock_on_Hand__c != 0){ | |
Case c = new Case(); | |
c.Description = stockItem.Item_Name__c + ' has been deleted with ' + stockItem.Stock_on_Hand__c + ' remaining. The item ID is ' + stockItem.Id; | |
casesToInsert.add(c); | |
} | |
} | |
insert casesToInsert; | |
} | |
//We are declaring method called getLowStockItems that returns a list of items where the item stock is low; | |
public static List <Stock_Item__c> getLowStockItems (){ | |
//We are declaring a list of low Stock items that is assigned to a SOQL statement to return the Item Name, Item Stock is Low, Minimum Stock Level and Stock on Hand from the Stock Item object where the Item Stock is Low field is true; | |
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 Item_Stock_is_Low__c = TRUE]; | |
System.debug('Low stock items: ' + lowStockItems); | |
return lowStockItems; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment