Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger StockItemTrigger on Stock_Item__c (before insert, before delete) {
- //Instantiate StockItemHandler
- //Before Insert Handling
- if(Trigger.isInsert && Trigger.isBefore) {
- //call the class in your handler for before insert
- Map<String, Stock_Item__c> stockItemMap =new Map<String, Stock_Item__c>();
- for (Stock_Item__c a : Trigger.new) {
- if ((a.Item_Name__c !=null) &&
- (System.Trigger.isInsert ||
- (a.Item_Name__c !=
- System.Trigger.oldMap.get(a.Id).Item_Name__c))){
- // Make sure another new name isn't also a duplicate;
- if (stockItemMap.containsKey(a.Item_Name__c)){
- a.Item_Name__c.addError('Another Stock Item exists with this name.');
- }else{
- stockItemMap.put(a.Item_Name__c, a);
- }
- }
- }
- // Find all the names in the database that have the same name as any of the stock items being inserted.;
- for (Stock_Item__c a : [SELECT Item_Name__c FROM Stock_Item__c WHERE Item_Name__c IN :stockItemMap.KeySet()]){
- Stock_Item__c newa = stockItemMap.get(a.Item_Name__c);
- newa.Item_Name__c.addError('A stock item with the same name already exists');
- }
- }
- //Before Delete Handling
- if(Trigger.isDelete && Trigger.isBefore) {
- //call the class in your handler for before delete
- //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
- //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];
- //create a list to hold any new cases we may create
- List<Case> newCases = new List<Case>();
- //Now go through each Stock Item and see if they need a case
- Map<String, Stock_Item__c> stockItemMap =new Map<String, Stock_Item__c>();
- for (Stock_Item__c a: Trigger.old) {
- if (a.Stock_on_Hand__c != 0) {
- System.debug('zeroStockonHand');
- Case cse = new Case();
- cse.Status = 'New';
- cse.Origin = 'Stock Item Issue';
- cse.Subject = 'Stock Item '+ 'Stock Item Name ' + a.Item_Name__c + '+,is not at 0 but is being deleted';
- 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;
- newCases.add(cse);
- }
- }
- insert newCases;
- }
- }
Add Comment
Please, Sign In to add comment