You need to sign in to do that
Don't have an account?
Joe Rodden 7
Apex Error Not Firing
Hi,
The class below is supposed to fire an error message if the Total of Sales Invoices goes over the opportunity Amount of the opp they're associated to. For some reason it's just doing nothing and I've been wracking my brain to figure out why. I'm fairly new to development so I'm sure it's something basic.
Trigger:
Class:
The class below is supposed to fire an error message if the Total of Sales Invoices goes over the opportunity Amount of the opp they're associated to. For some reason it's just doing nothing and I've been wracking my brain to figure out why. I'm fairly new to development so I'm sure it's something basic.
Trigger:
trigger MasterSalesInvoiceTrigger on Sales_Invoice__c ( before insert, after insert, before update, after update, before delete, after delete, after undelete) { if (Trigger.isBefore) { if (Trigger.isInsert) { SalesInvoice_InvoiceTotalValidation.siTotalValidation(Trigger.new, Trigger.oldMap); } if (Trigger.isUpdate) { SalesInvoice_InvoiceTotalValidation.siTotalValidation(Trigger.new, Trigger.oldMap); } if (Trigger.isDelete) { } } if (Trigger.IsAfter) { if (Trigger.isInsert) { } if (Trigger.isUpdate) { } if (Trigger.isDelete) { } if (Trigger.isUndelete){ } } }
Class:
public class SalesInvoice_InvoiceTotalValidation { public static void siTotalValidation(List<Sales_Invoice__c> trigSi, Map<Id, Sales_Invoice__c> oldSIMap) { Map<Id, Decimal> siTotal = new Map<Id, Decimal>(); //Holds the sum of all inserted and updated sales invoices Map<Id, Decimal> oppAmount = new Map<Id, Decimal>(); //Hold the current amount for the opportunity Map<Id, List<Sales_Invoice__c>> oppIdToSalesInvoice = new Map<Id, List<Sales_Invoice__c>>(); //Maps the opp Id to the Sales Invoices for the error message Set<Id> oppIdSet = new Set<Id>(); //Used to create the key for siTotal and oppAmount when calling error //Filling up maps for (Sales_Invoice__c si : trigSi) { if (siTotal.containsKey(si.Opportunity__c)) { siTotal.put(si.Opportunity__c, si.Total__c + siTotal.get(si.Opportunity__c)); } else { siTotal.put(si.Opportunity__c, si.Total__c); } if(oppIdToSalesInvoice.containsKey(si.Opportunity__c)) { oppIdToSalesInvoice.get(si.Opportunity__c).add(si); } else { oppIdToSalesInvoice.put(si.Opportunity__c, new List<Sales_Invoice__c>{si}); } oppAmount.put(si.Opportunity__c, si.Opportunity__r.Amount); } oppIdSet = siTotal.keySet(); for(Id oppId : oppIdSet) { if(siTotal.get(oppId) > oppAmount.get(oppId)){ for(Sales_Invoice__c si2 : oppIdToSalesInvoice.get(oppId)) { si2.addError('Total of all Sales Invoices cant be greater than opportunity amount. Adjust opp amount first then adjust the Sales Invoices.'); } } } } }
What is the event you are testing ? insert or update