Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Created April 16, 2018 05:13
Show Gist options
  • Save olgaloza/b8374b69c57143486462e1d8996b5f0f to your computer and use it in GitHub Desktop.
Save olgaloza/b8374b69c57143486462e1d8996b5f0f to your computer and use it in GitHub Desktop.
Final Project - Handler Class
//A Utility class to process Stock Item records from the Stock Item Handler
public with sharing class StockItemHandler {
//Default Constructor
public StockItemHandler() {
}
//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
public static void onBeforeInsert(List<Stock_Item__c> newStockItems) {
//Before a Stock Item can be created, check that there is not already a stock item record with a matching name.
//If there is already a Stock Item with that name, change the name so that it is the name entered,
//plus the words “Duplicate Item”. We have a process in place to check for these elsewhere.
//
//first create a list of all existing Item Names in the Org.
List<Stock_Item__c> allStockItems = new List<Stock_Item__c>([SELECT Item_Name__c FROM Stock_Item__c]);
//loop through the Items we are trying to create and
for (Stock_Item__c newItem : newStockItems){
//loop through existing
for (Stock_Item__c item : allStockItems) {
//compare new and existing name to check if each new Item Name matches any from the existing list
if (newItem.Item_Name__c == item.Item_Name__c) {
/******option 1 with renaming:
//if yes, append "duplicate item" to the name
newItem.Item_Name__c = item.Item_Name__c + ': Duplicate Item';
*/
/******option 2 with exception*/
newItem.addError('Duplicate name found! Fix & resubmit.', true);
}
}
//No DML needed here, since it's a BEFORE insert trigger
//}
}
}
public static void onBeforeDelete(List<Stock_Item__c> deleteStockItems, Map<Id, Stock_Item__c> deletedStockItemsMap) {
//Before a Stock Item is deleted, make sure that the stock on hand is at 0.
//If it is not, Create a case so that someone is alerted.
//The case should indicate the name of the item that was deleted, the id,
//and the number of stock that were on hand when it was deleted in the description.
//The rest of the case can be configured however you think best.
//make a list to hold Cases for bulkifying
List<Case> createCases = new List<Case>();
//make a list of items to delete - or use the List<Stock_Item__c> deleteStockItems
//loop through the list and check stock levels
for(Stock_Item__c Item : deleteStockItems){
if(Item.Stock_on_Hand__c > 0) {
//create a Case
System.debug(Item.Item_Name__c + ' has stock on hand');
Case cse = new Case();
cse.Status = 'New';
cse.Origin = 'Internal';
cse.Subject = 'Clean up stock for "' + Item.Item_Name__c + '" (' + Item.Id + ')';
cse.Description = 'Stock item "'+Item.Item_Name__c+'" was deleted but it has '+Item.Stock_on_Hand__c+' in stock.';
createCases.add(cse);
}
}
insert createCases;
}
public static List<Stock_Item__c> getLowStockItems(){
/*
We need a method that can be called from elsewhere in our codebase called getLowStockItems
This should return a list of all the Stock Items that have a stock on hand count
at or below their minimum stock level.
It should include the following fields for the Stock Items it returns:
ID
Item_Name__c
Item_Stock_is_Low__c
Minimum_Stock_Level__c
Stock_on_Hand__c
*/
//make a list of all stock items that meet the criteria
List<Stock_Item__c> lowStockItems = new List<Stock_Item__c>(
[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]
);
//if anything is found, output details to debug log
if(lowStockItems.size() > 0){
System.debug('Found '+lowStockItems.size()+' items with low inventory:');
//Loop through the low stock items list and output to debug log
for(Stock_Item__c item :lowStockItems){
System.debug(item.Item_Name__c+', ID:'+item.Id+', Min: '+item.Minimum_Stock_Level__c+', Inv: '+item.Stock_on_Hand__c);
}
} else
System.debug('Inventory levels are okay!');
return lowStockItems;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment