+ Start a Discussion
Joe Rodden 7Joe 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:
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.');
                }
            }
        }
        
        
        
    }
}

 
Raj VakatiRaj Vakati
Can you add some debug  logs and see where its failing .. 

What is the event you are testing ? insert or update 
Joe Rodden 7Joe Rodden 7
I'm trying to make it fire both on insert and update. Below is the debug log. It looks like possible line 28 the oppId isn't finding an Opportunity? Does this need to be after insert since we need the associated Opportunity ID?
 
44.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
07:22:52.0 (375885)|USER_INFO|[EXTERNAL]|00516000005IxIz|jrodden@gocatalant.com.fcsb|Eastern Standard Time|GMT-05:00
07:22:52.0 (413383)|EXECUTION_STARTED
07:22:52.0 (417522)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
07:22:52.0 (1126112)|CODE_UNIT_STARTED|[EXTERNAL]|01q3F0000006CBI|MasterSalesInvoiceTrigger on Sales_Invoice trigger event BeforeInsert|__sfdc_trigger/MasterSalesInvoiceTrigger
07:22:52.0 (1168100)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
07:22:52.0 (1505448)|HEAP_ALLOCATE|[72]|Bytes:3
07:22:52.0 (1535651)|HEAP_ALLOCATE|[77]|Bytes:152
07:22:52.0 (1547590)|HEAP_ALLOCATE|[342]|Bytes:408
07:22:52.0 (1559506)|HEAP_ALLOCATE|[355]|Bytes:408
07:22:52.0 (1570208)|HEAP_ALLOCATE|[467]|Bytes:48
07:22:52.0 (1595132)|HEAP_ALLOCATE|[139]|Bytes:6
07:22:52.0 (1622076)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
07:22:52.0 (1638089)|VARIABLE_SCOPE_BEGIN|[1]|this|MasterSalesInvoiceTrigger|true|false
07:22:52.0 (1741193)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x4a46953f
07:22:52.0 (1810466)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
07:22:52.0 (1855047)|VARIABLE_SCOPE_BEGIN|[1]|this|MasterSalesInvoiceTrigger|true|false
07:22:52.0 (1869606)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x4a46953f
07:22:52.0 (1876780)|STATEMENT_EXECUTE|[1]
07:22:52.0 (1902850)|HEAP_ALLOCATE|[50]|Bytes:5
07:22:52.0 (1925490)|HEAP_ALLOCATE|[56]|Bytes:5
07:22:52.0 (1934636)|HEAP_ALLOCATE|[64]|Bytes:7
07:22:52.0 (1956566)|STATEMENT_EXECUTE|[7]
07:22:52.0 (1961681)|STATEMENT_EXECUTE|[8]
07:22:52.0 (1963698)|STATEMENT_EXECUTE|[10]
07:22:52.0 (15842605)|HEAP_ALLOCATE|[10]|Bytes:1
07:22:52.0 (15867572)|METHOD_ENTRY|[1]|01p3F0000015hov|SalesInvoice_InvoiceTotalValidation.SalesInvoice_InvoiceTotalValidation()
07:22:52.0 (15874041)|STATEMENT_EXECUTE|[1]
07:22:52.0 (15878746)|STATEMENT_EXECUTE|[1]
07:22:52.0 (15883076)|METHOD_EXIT|[1]|SalesInvoice_InvoiceTotalValidation
07:22:52.0 (15944964)|METHOD_ENTRY|[10]|01p3F0000015hov|SalesInvoice_InvoiceTotalValidation.siTotalValidation(List<Sales_Invoice__c>, Map<Id,Sales_Invoice__c>)
07:22:52.0 (15974357)|VARIABLE_SCOPE_BEGIN|[3]|trigSi|List<Sales_Invoice__c>|true|false
07:22:52.0 (32563030)|VARIABLE_ASSIGNMENT|[3]|trigSi|[{"IsDeleted":false,"Total_Amount_Received__c":0.00,"Opportunity_Name__c":"test","Invoice_Goes_To__c":"Not Applicable","Opportunity__c":"0063F00000CKkMXQA1","Weighted_Over_Underdue_Score__c":0.00,"Total__c":1000.00,"Name":"eerw","Total_Payments_Received__c":0.00,"Sales_Invoice_Count__c":1.00}]|0x2d306776
07:22:52.0 (32583280)|VARIABLE_SCOPE_BEGIN|[3]|oldSIMap|Map<Id,Sales_Invoice__c>|true|false
07:22:52.0 (32592492)|VARIABLE_ASSIGNMENT|[3]|oldSIMap|null|
07:22:52.0 (32600172)|STATEMENT_EXECUTE|[3]
07:22:52.0 (32602332)|STATEMENT_EXECUTE|[4]
07:22:52.0 (32623942)|HEAP_ALLOCATE|[4]|Bytes:4
07:22:52.0 (32645948)|VARIABLE_SCOPE_BEGIN|[4]|siTotal|Map<Id,Decimal>|true|false
07:22:52.0 (32670132)|VARIABLE_ASSIGNMENT|[4]|siTotal|{}|0x5e35d448
07:22:52.0 (32677242)|STATEMENT_EXECUTE|[5]
07:22:52.0 (32685443)|HEAP_ALLOCATE|[5]|Bytes:4
07:22:52.0 (32691768)|VARIABLE_SCOPE_BEGIN|[5]|oppAmount|Map<Id,Decimal>|true|false
07:22:52.0 (32708143)|VARIABLE_ASSIGNMENT|[5]|oppAmount|{}|0x78459f12
07:22:52.0 (32714231)|STATEMENT_EXECUTE|[6]
07:22:52.0 (32754434)|HEAP_ALLOCATE|[6]|Bytes:4
07:22:52.0 (32786836)|VARIABLE_SCOPE_BEGIN|[6]|oppIdToSalesInvoice|Map<Id,List<Sales_Invoice__c>>|true|false
07:22:52.0 (32798899)|VARIABLE_ASSIGNMENT|[6]|oppIdToSalesInvoice|{}|0x52c90c8c
07:22:52.0 (32804295)|STATEMENT_EXECUTE|[7]
07:22:52.0 (32816729)|HEAP_ALLOCATE|[7]|Bytes:4
07:22:52.0 (32887606)|VARIABLE_SCOPE_BEGIN|[7]|oppIdSet|Set<Id>|true|false
07:22:52.0 (32918545)|VARIABLE_ASSIGNMENT|[7]|oppIdSet|[]|0x22ae0509
07:22:52.0 (33102073)|HEAP_ALLOCATE|[10]|Bytes:5
07:22:52.0 (33129704)|HEAP_ALLOCATE|[10]|Bytes:4
07:22:52.0 (33139726)|VARIABLE_SCOPE_BEGIN|[10]|si|Sales_Invoice__c|true|false
07:22:52.0 (33656183)|VARIABLE_ASSIGNMENT|[10]|si|{"IsDeleted":false,"Total_Amount_Received__c":0.00,"Opportunity_Name__c":"test","Invoice_Goes_To__c":"Not Applicable","Opportunity__c":"0063F00000CKkMXQA1","Weighted_Over_Underdue_Score__c":0.00,"Total__c":1000.00,"Name":"eerw","Total_Payments_Received__c":0.00,"Sales_Invoice_Count__c":1.00}|0x76fb2f0f
07:22:52.0 (33670425)|STATEMENT_EXECUTE|[10]
07:22:52.0 (33738904)|STATEMENT_EXECUTE|[13]
07:22:52.0 (33741223)|STATEMENT_EXECUTE|[14]
07:22:52.0 (33789914)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:-4
07:22:52.0 (33819192)|STATEMENT_EXECUTE|[19]
07:22:52.0 (33821363)|STATEMENT_EXECUTE|[20]
07:22:52.0 (33834173)|HEAP_ALLOCATE|[20]|Bytes:4
07:22:52.0 (33864347)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
07:22:52.0 (33869062)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:-4
07:22:52.0 (33875229)|STATEMENT_EXECUTE|[23]
07:22:52.0 (33913648)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:-4
07:22:52.0 (33929450)|HEAP_ALLOCATE|[10]|Bytes:5
07:22:52.0 (33942862)|VARIABLE_ASSIGNMENT|[10]|si|null|
07:22:52.0 (33948848)|STATEMENT_EXECUTE|[27]
07:22:52.0 (33974244)|HEAP_ALLOCATE|[27]|Bytes:26
07:22:52.0 (33991896)|VARIABLE_ASSIGNMENT|[27]|oppIdSet|["0063F00000CKkMXQA1"]|0x6f3908eb
07:22:52.0 (34008020)|HEAP_ALLOCATE|[28]|Bytes:4
07:22:52.0 (34043162)|HEAP_ALLOCATE|[28]|Bytes:4
07:22:52.0 (34069111)|HEAP_ALLOCATE|[28]|Bytes:5
07:22:52.0 (34101070)|HEAP_ALLOCATE|[28]|Bytes:4
07:22:52.0 (34106668)|VARIABLE_SCOPE_BEGIN|[28]|oppId|Id|false|false
07:22:52.0 (34287912)|VARIABLE_ASSIGNMENT|[28]|oppId|"0063F00000CKkMXQA1"
07:22:52.0 (34295856)|STATEMENT_EXECUTE|[28]
07:22:52.0 (34333615)|HEAP_ALLOCATE|[29]|Bytes:28
07:22:52.0 (34350213)|STATEMENT_EXECUTE|[29]
07:22:52.0 (34358331)|HEAP_ALLOCATE|[28]|Bytes:5
07:22:52.0 (34368748)|VARIABLE_ASSIGNMENT|[28]|oppId|null
07:22:52.0 (34379467)|METHOD_EXIT|[10]|01p3F0000015hov|SalesInvoice_InvoiceTotalValidation.siTotalValidation(List<Sales_Invoice__c>, Map<Id,Sales_Invoice__c>)
07:22:52.0 (34395938)|STATEMENT_EXECUTE|[12]
07:22:52.0 (34402094)|STATEMENT_EXECUTE|[16]
07:22:52.0 (34406850)|STATEMENT_EXECUTE|[21]
07:22:52.34 (34412143)|CUMULATIVE_LIMIT_USAGE
07:22:52.34 (34412143)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

07:22:52.34 (34412143)|CUMULATIVE_LIMIT_USAGE_END

07:22:52.0 (35271423)|CODE_UNIT_FINISHED|MasterSalesInvoiceTrigger on Sales_Invoice trigger event BeforeInsert|__sfdc_trigger/MasterSalesInvoiceTrigger
07:22:52.0 (358040540)|CODE_UNIT_STARTED|[EXTERNAL]|01q3F0000006CBI|MasterSalesInvoiceTrigger on Sales_Invoice trigger event AfterInsert|__sfdc_trigger/MasterSalesInvoiceTrigger
07:22:52.0 (358092390)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
07:22:52.0 (358180540)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
07:22:52.0 (358193637)|VARIABLE_SCOPE_BEGIN|[1]|this|MasterSalesInvoiceTrigger|true|false
07:22:52.0 (358238381)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x1140c2ea
07:22:52.0 (358286158)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
07:22:52.0 (358293952)|VARIABLE_SCOPE_BEGIN|[1]|this|MasterSalesInvoiceTrigger|true|false
07:22:52.0 (358309984)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x1140c2ea
07:22:52.0 (358319742)|STATEMENT_EXECUTE|[1]
07:22:52.0 (358331602)|STATEMENT_EXECUTE|[7]
07:22:52.0 (358337696)|STATEMENT_EXECUTE|[21]
07:22:52.0 (358344695)|STATEMENT_EXECUTE|[22]
07:22:52.0 (358350432)|STATEMENT_EXECUTE|[25]
07:22:52.0 (358355950)|STATEMENT_EXECUTE|[28]
07:22:52.0 (358365134)|STATEMENT_EXECUTE|[31]
07:22:52.358 (358369459)|CUMULATIVE_LIMIT_USAGE
07:22:52.358 (358369459)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10