0

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;  
     } 

}

2 Answers 2

1

The system will not penalize you (e.g. DML statement governor limits increase) for inserting an empty list, nor will you get an exception.

You don't need to check, and in fact, should not check, as it just costs more code and CPU time. In other words, you're doing it the way that I'd recommend you do it.

2
  • yeah I still need to add a check tho. and I don't know how to do it. I didn't think I need to check either. This is for a class Im taking. Commented May 6, 2021 at 13:25
  • @LeahGustafson For learning purposes, it's just if(addCases.size() > 0) { insert addCases; } Just remember not to do that in production code.
    – sfdcfox
    Commented May 6, 2021 at 14:24
1

sfdcfox has the correct answer; please accept his, not mine

since it is clear you are on the wonderful trail of learning apex, may I suggest (though off-point to your question) a somewhat cleaner code (variable names, fewer statements)

public class StockItemsService { 

 
  public void alertWhenOutOfStock (List <Stock_Item__c> stockItems) {
    //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.
    
    List<Case> outOfStockCases = new List<Case>();
    
      for(Stock_Item__c sc : stockItems){
          if(sc.stock_on_hand__c !=0){
              outOfStockCases.add(new Case(
                Subject = sc.Item_Name__c + ' Deletion Mistake?',
                Description = sc.Item_Name__c + ' was deleted and the stock level was ' + 
                              sc.Stock_on_hand__c +'. Item Id number: ' + sc.Id));
            
          }
      }
      insert outOfStockCases;  
  } 

}
  • Use plurals for collection variables
  • name your methods to reflect business purpose; name the class with an eye to the future (additional methods about stockitems)
  • instance methods should normally be your first choice
  • name your variables to reflect intent
  • take advantage of the ability to set fields in an sobject without intermediate variables
  • indent properly
1
  • ok, thank you. I will do that. Commented May 6, 2021 at 14:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .