Guest User

Untitled

a guest
Dec 16th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. trigger StockItemTrigger on Stock_Item__c (before insert, before delete) {
  2. //Instantiate StockItemHandler
  3. //Before Insert Handling
  4.  
  5. if(Trigger.isInsert && Trigger.isBefore) {
  6. //call the class in your handler for before insert
  7. Map<String, Stock_Item__c> stockItemMap =new Map<String, Stock_Item__c>();
  8. for (Stock_Item__c a : Trigger.new) {
  9. if ((a.Item_Name__c !=null) &&
  10. (System.Trigger.isInsert ||
  11. (a.Item_Name__c !=
  12. System.Trigger.oldMap.get(a.Id).Item_Name__c))){
  13. // Make sure another new name isn't also a duplicate;
  14. if (stockItemMap.containsKey(a.Item_Name__c)){
  15. a.Item_Name__c.addError('Another Stock Item exists with this name.');
  16. }else{
  17. stockItemMap.put(a.Item_Name__c, a);
  18. }
  19. }
  20. }
  21. // Find all the names in the database that have the same name as any of the stock items being inserted.;
  22. for (Stock_Item__c a : [SELECT Item_Name__c FROM Stock_Item__c WHERE Item_Name__c IN :stockItemMap.KeySet()]){
  23. Stock_Item__c newa = stockItemMap.get(a.Item_Name__c);
  24. newa.Item_Name__c.addError('A stock item with the same name already exists');
  25.  
  26. }
  27.  
  28. }
  29.  
  30. //Before Delete Handling
  31. if(Trigger.isDelete && Trigger.isBefore) {
  32. //call the class in your handler for before delete
  33. //Create methods here to handle the before insert, before delete and utility processes described in the requirements
  34. //They should accept lists of Stock_Item__c records from the trigger
  35. //List<Stock_Item__c> availableStock = [SELECT Id, Name, OwnerId, Item_Name__c , Stock_on_Hand__c FROM Stock_Item__c WHERE Stock_on_Hand__c != 0];
  36. //create a list to hold any new cases we may create
  37.  
  38. List<Case> newCases = new List<Case>();
  39. //Now go through each Stock Item and see if they need a case
  40. Map<String, Stock_Item__c> stockItemMap =new Map<String, Stock_Item__c>();
  41. for (Stock_Item__c a: Trigger.old) {
  42. if (a.Stock_on_Hand__c != 0) {
  43. System.debug('zeroStockonHand');
  44. Case cse = new Case();
  45. cse.Status = 'New';
  46. cse.Origin = 'Stock Item Issue';
  47. cse.Subject = 'Stock Item '+ 'Stock Item Name ' + a.Item_Name__c + '+,is not at 0 but is being deleted';
  48. cse.Description = 'Stock Item Name ' + a.Item_Name__c + ', Stock Id ' + a.Name + ', Stock Item SF Id ' + a.Id + '. Stock on hand ' + a.Stock_on_Hand__c;
  49. newCases.add(cse);
  50.  
  51. }
  52.  
  53. }
  54.  
  55.  
  56.  
  57. insert newCases;
  58.  
  59.  
  60.  
  61. }
  62.  
  63. }
Add Comment
Please, Sign In to add comment