How do I add a check to see if any cases need to be inserted before processing the insert statement in the code below?
public class StockItemDeleter {
// declare a method, which accepts a list of stock items as a call this ListStockItems
public static void StockMethodDeleter (List <Stock_Item__c> ListStockItems) {
//before an item is deleted, check if the stock is at 0.
//if it is not at zero, create a case.
//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.
system.debug('ListSTockItems:' + listStockItems);
List<Case> addCases = new List<Case>();
For(Stock_Item__c sc : ListStockItems){
if(sc.stock_on_hand__c !=0){
Case UhOh = new Case();
UhOh.Subject = sc.Item_Name__c + ' Deletion Mistake?';
UhOh.Description = sc.Item_Name__c + ' was deleted and the stock level was ' +
sc.Stock_on_hand__c +'. Item Id number: '
+ sc.Id;
addCases.add(UhOh);
system.debug('Case'+ UhOh);
}
}
insert addCases;
}
}