Skip to content

Instantly share code, notes, and snippets.

@asmitakhare
Last active October 14, 2020 00:16
Show Gist options
  • Save asmitakhare/1f850e09d13c7f0f6d9964ba7ac151c7 to your computer and use it in GitHub Desktop.
Save asmitakhare/1f850e09d13c7f0f6d9964ba7ac151c7 to your computer and use it in GitHub Desktop.
StockItemHandler.Apxc FinalProject.Asmita
/**
* A Utility class to process Stock Item records from the Stock Item Handler
*/
public with sharing class StockItemHandler {
/**
* Class 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
// creating a public method here to handle the before insert and if there is an existing record with the same name I add error
public static void onBeforeInsert(List<Stock_Item__c> listOfStockIteam) {
//instantiating the set of string as it is more efficient then list
Set<String> itemNames = new Set<String>();
//looping throug the listofstockiteams
for (Stock_Item__c item : listOfStockIteam)
//adding the all item name we got it through for loop in the set itemnames
itemNames.add(item.Item_Name__c);
//creating the another set of string
Set<String> foundNames = new Set<String>();
//looping through and checking the exisitng database with itemsnames
for (Stock_Item__c item : [SELECT Name, Item_Name__c FROM Stock_Item__c WHERE Item_Name__c IN : itemNames])
//adding the record in the set named foundNames
foundNames.add(item.Item_Name__c);
//looping through the list of stock items
for(Stock_Item__c item : listOfStockIteam)
if(foundNames.contains(item.Item_Name__c))
//if a duplicate is found then mark as 'cannot create the duplicate Item name
item.Name.adderror('You cannnot create the duplicate Item name : '+ item.Item_Name__c);
}
// creating a public method to handle the before delete and making sure that stock on hand is more than 0
public static void onBeforeDelete(List<Stock_Item__c> listOfStockIteam) {
//printing the list of stock items
System.debug(listOfStockIteam);
//creating list of case, we can use this list for bulkification
List<Case> listOfCase = new List<Case>();
for(Stock_Item__c deleteStock : listOfStockIteam){
if(deleteStock.Stock_on_Hand__c != 0){
//The case is indicating the name of the items that was deleted
Case createCase = new Case();
createCase.Origin = 'Web';
createCase.Status = 'New';
createCase.Subject = 'Case created for Stock Item with Number : '+ deleteStock.Name +', Name : '+ deleteStock.Item_Name__c;
createCase.Description = 'Following Stock Item is deleted but Stock on Hand is more than zero '+ '\r\n'
+'Stock Item Id : '+ deleteStock.Id+'\r\n'
+'Item Name : '+ deleteStock.Item_Name__c+'\r\n'
+'Stock on Hand : '+String.valueOf(deleteStock.Stock_on_Hand__c) ;
//adding new case record to the list of cases
listOfCase.add(createCase);
}
}
if(listOfCase.size() > 0){
insert listOfCase;// calling dml operation outside the for loop
}
}
}
@isTest
private class StockItemHandler_Test {
@isTest
static void testBeforeInsertFunctionality() {
// Implement test code
Stock_Item__c newSt1 = new Stock_Item__c();
newSt1.Item_Name__c = 'Trav';
insert newSt1;
try{
Stock_Item__c newSt2 = new Stock_Item__c();
newSt2.Item_Name__c = 'Trav';
insert newSt2;// insert should throw an error
//This system assert should not be executed as duplicte stock item insert should throw an exception but if it does, then assert will fail.
System.assert(false, 'Duplicate validation failed.');
}
//catch block will catch an exception
catch(System.DmlException ex){
//The DmlException message will have 'Insert failed.' phrase.
System.assert(ex.getMessage().contains('Insert failed.'), 'DmlException does not contain "Insert Failed" phrase');
}
}
@isTest
static void testBeforeDeleteFunctionalityCaseCreation() {
// Implement test code
//insert the records
Stock_Item__c stoItem = new Stock_Item__c ();
String itemName = 'Red';
stoItem.Item_Name__c = itemName;
stoItem.Minimum_Stock_Level__c = 12;
stoItem.Stock_on_Hand__c = 10;// setting stock on hands more than 0
insert stoItem;
String searchItemName = '%'+itemName+'%';// used LIKE operator
//delete the record
delete stoItem;
//creating the SOQL Query to get the case record with Subject like searchItemName. I refered for like operator in soql https://developer.salesforce.com/forums/?id=906F0000000MNwBIAW
Integer afterDeleteCaseCount = [SELECT COUNT() FROM Case WHERE Subject LIKE :searchItemName ];
//If assert succeed will show the message itemName
System.assertEquals(afterDeleteCaseCount,1,'The case with '+itemName+' not found');
}
@isTest
static void testGetLowStockItems() {
// Implement test code
// 1. insert new stock item with minimum stock 6 and stock on hand 1
Stock_Item__c stockItem1 = new Stock_Item__c();
stockItem1.Item_Name__c = 'Handler';
stockItem1.Minimum_Stock_Level__c = 6;
stockItem1.Stock_on_Hand__c = 1;
insert stockItem1;
//2. calling StockItemHelper.getLowStockItems();
List<Stock_Item__c> stockList = StockItemHelper.getLowStockItems();
Boolean isItemFound = false;//instantiating boolean is false if we don't find item
for (Stock_Item__c si : stockList)//looping through the list
{
if (si.id==stockItem1.id) //checking the stockItem1 id in the list
{
isItemFound = true;
}
}
// 3. if id doesn't find it then assert fail.but in this case it will find and assert will be succesful
System.assert(isItemFound==true, 'low stock item not found');
}
@isTest
static void packageUploadCoverage() {
Stock_Item__c item = new Stock_Item__c();
item.Item_Name__c = 'Test';
item.Description__c = 'Test';
item.Minimum_Stock_Level__c =3;
insert item;
}
}
public class StockItemHelper {
//In this method we are getting records by using SOQL query where Stock on hands field value is lower than the Minimum stock level field.
public static List<Stock_Item__c> getLowStockItems (){
return [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];
}
}
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
else if(Trigger.isDelete && Trigger.isBefore) {
//call the class in your handler for before delete. Returns a list of the old versions of the sObject records.
//This sObject list is only available in update and delete triggers. Developer guide.
StockItemHandler.onBeforeDelete(Trigger.old);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment