Skip to content

Instantly share code, notes, and snippets.

@AngelOfCA
Last active May 23, 2019 00:36
Show Gist options
  • Save AngelOfCA/d40506b5902e6eba6bfeb7c78f1371b9 to your computer and use it in GitHub Desktop.
Save AngelOfCA/d40506b5902e6eba6bfeb7c78f1371b9 to your computer and use it in GitHub Desktop.
/**
* A Utility class to process Stock Item records from the Stock Item Handler
*/
public with sharing class StockItemHandler {
/**
* Class constructor.
*/
public StockItemHandler() {
}
public static void checkDuplicateName(List<Stock_Item__c> newStockItem) {
// Need to store existing list of items in a set.
Set<String> listOfItems = new Set<String>();
for(Stock_Item__c listOfStock : [Select ID, Item_Name__c
From Stock_Item__c]){
listOfItems.add(listOfStock.Item_Name__c);
}
// check if new item name already exists - if so, produce an error
for(Stock_Item__c listOfStock: newStockItem){
if(listOfItems.contains(listOfStock.Item_Name__c)){
listOfStock.Item_Name__c.adderror('This item already exists.');
}
}
}
public static void deleteStockItems(List <Stock_Item__c> SID)
{
//create a list of new cases
list <Case> newCases = new List<Case>();
for(Stock_Item__c StockItemsDelete: SID)
{
// Before stock item is deleted - check if Stock on hand is not equal to 0
// and add a new case alert someone to check the stock.
If(StockItemsDelete.Stock_on_Hand__c != 0)
{
Case c = new Case ();
c.comments = StockItemsDelete.id +' '+ StockItemsDelete.Item_Name__c +' '+ StockItemsDelete.Stock_on_Hand__c;
c.status = 'New';
c.Origin = 'Not Zeroed Stock Item';
c.Subject = 'Please review '+ StockItemsDelete.Item_Name__c +' Inventory Level';
newCases.add(c);
}
}
insert newCases;
}
public static list<Stock_Item__c> getLowStockItems() {
// Create list of stock items where Item Stock Is Low equals true.
List<Stock_Item__c> STIS = [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' + STIS);
// Return list of items that are low stock
return STIS;
}
//Create methods here to handle the before insert, before delete and utility processes described in the requirements
//They should accept lists of Stock_Item__c records from the trigger
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment