• Stephanie Arce
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
When I try to do a mass update of records, this trigger fails on about half of them on line 56, "argument cannot be null".

When these records are created, Invoice__c isn't populated until later. I have no problem creating the records, it's just when I try to update them to give Invoice__c a value that half fail. Cost__c is a required field and never null.

I've checked through a debug log that I'm not close to any limits. Can anyone help me figure out what's wrong?

Here is the trigger:
trigger Line_Item_Invoice_RollupTrigger on Line_Item__c (after update, after insert, after delete) {
     Map<Id, Double> LineItemTotal = new Map<Id, Double>();
     Map<Id, Double> LineItemTotalAdjusted = new Map<Id, Double>();
     Map<Id, Double> NumberofLineItems = new Map<Id, Double>();
     Double count = 0;
     List<Invoice__c> invoicesToUpdate = new List<Invoice__c>();
     List<Line_Item__c> itemsForInvoice;
     List<Line_Item__c> items;
     if (trigger.isDelete) {
         items = trigger.old;
     }
     else {
         items = trigger.new;
    }
    
    // Go through all line items that the trigger is acting on.
    Set<ID> setInvoiceId = new Set<ID>();
    for (Line_Item__c triggerItem: items) {       

            // Get the invoice's ID for the line item and then
            // ensure its ID exists in the line item / line item adjusted totals
            // maps and their totals default to $0.00.        
        if (!LineItemTotal.containsKey(triggerItem.Invoice__c)) {
            LineItemTotal.put(triggerItem.Invoice__c, 0.0);
        }
        if (!LineItemTotalAdjusted.containsKey(triggerItem.Invoice__c)) {
            LineItemTotalAdjusted.put(triggerItem.Invoice__c, 0.0);
        }
        if (!NumberofLineItems.containsKey(triggerItem.Invoice__c)) {
            NumberofLineItems.put(triggerItem.Invoice__c, 0.0);
        }
        
        
        
        if(triggerItem.Invoice__c != NULL)
            setInvoiceId.add(triggerItem.Invoice__c);
    }
    
    // Sum the total cost of the line items
    // for the current invoice (see invoice above).
    //
    // 1. Get all of the line items for the invoice.
    // 2. For each line item update the mapping for the invoice 
    //    to be (previous total + total cost).    
    List<Line_Item__c> lstInvLineItem_LineItemTotal = new List<Line_Item__c>();
    
    if(setInvoiceId.size()>0){
        lstInvLineItem_LineItemTotal = [SELECT Id, Cost__c, Total_Cost_incl_Adjustment__c, Invoice__c FROM Line_Item__c
                WHERE Invoice__c IN :setInvoiceId];

    }
    // If the list has records, add those IDs and totals to the map.
    if(lstInvLineItem_LineItemTotal.size()>0){
        for(Line_Item__c item:lstInvLineItem_LineItemTotal){ 
            if(item.Invoice__c != NULL && LineItemTotal.containsKey(item.Invoice__c))    {
                LineItemTotal.put(item.Invoice__c, LineItemTotal.get(item.Invoice__c) + item.Cost__c); 
                LineItemTotalAdjusted.put(item.Invoice__c, 
                    LineItemTotalAdjusted.get(item.Invoice__c) + item.Total_Cost_incl_Adjustment__c); 
                NumberofLineItems.put(item.Invoice__c, NumberofLineItems.get(item.Invoice__c)+1);
            }
        }
    }
    
    List<Id> invoices = new List<Id>();
    for (Id previous : LineItemTotal.keyset()) {
        invoices.add(previous);
    }
    for (Id current : LineItemTotal.keyset()) {
        if (!LineItemTotal.containsKey(current)) {
            invoices.add(current);
        }
    }
    // Get a list of all invoices where its Id is in the invoices map.
    List<Invoice__c> lstInvoices = new List<Invoice__c>();
    if(invoices.size()>0){
        lstInvoices = [SELECT Id, Line_Item_Total__c, Line_Item_Total_Adjusted__c, Number_of_Line_Items__c
             FROM Invoice__c WHERE Id IN :invoices];
    }
    // Update the map with line item and line item adjusted totals.
    if(lstInvoices.size()>0){
        for(Invoice__c invoice:lstInvoices){
            if (LineItemTotal.containsKey(invoice.id)) {
                invoice.Line_Item_Total__c = LineItemTotal.get(invoice.id);
            }
            
            if (LineItemTotalAdjusted.containsKey(invoice.id)) {
                invoice.Line_Item_Total_Adjusted__c = LineItemTotalAdjusted.get(invoice.id);
            }
            
            if (NumberofLineItems.containsKey(invoice.id)) {
                invoice.Number_of_Line_Items__c = NumberofLineItems.get(invoice.id);
            }
            
            invoicesToUpdate.add(invoice);
        }
    }
    update invoicesToUpdate;
}

Can someone help me figure out what's wrong with this trigger? I'm writing it to create lookup rollup-summary esque fields on the parent object, Invoice__c, from the child Line_Item__c. I get this error though upon updating a record:

"Line_Item_Invoice_RollupTrigger: execution of AfterUpdate
caused by: System.NullPointerException: Argument cannot be null.
Trigger.Line_Item_Invoice_RollupTrigger: line 55, column 1"

Here's the trigger:
trigger Line_Item_Invoice_RollupTrigger on Line_Item__c (after update, after insert, after delete) {
     Map<Id, Double> LineItemTotal = new Map<Id, Double>();
     Map<Id, Double> LineItemTotalAdjusted = new Map<Id, Double>();
     Map<Id, Double> NumberofLineItems = new Map<Id, Double>();
     List<Invoice__c> invoicesToUpdate = new List<Invoice__c>();
     List<Line_Item__c> itemsForInvoice;
     List<Line_Item__c> items;
     if (trigger.isDelete) {
         items = trigger.old;
     }
     else {
         items = trigger.new;
    }
    // Go through all line items that the trigger is acting on.
    Set<ID> setInvoiceId = new Set<ID>();
    for (Line_Item__c triggerItem: items) {       
        
            // Get the invoice's ID for the line item and then
            // ensure its ID exists in the line item / line item adjusted totals
            // maps and their totals default to $0.00.        
        if (!LineItemTotal.containsKey(triggerItem.Invoice__c)) {
            LineItemTotal.put(triggerItem.Invoice__c, 0.0);
        }
        if (!LineItemTotalAdjusted.containsKey(triggerItem.Invoice__c)) {
            LineItemTotalAdjusted.put(triggerItem.Invoice__c, 0.0);
        }
        if (!NumberofLineItems.containsKey(triggerItem.Invoice__c)) {
            NumberofLineItems.put(triggerItem.Invoice__c, 0);
        }
        
        if(triggerItem.Invoice__c != NULL)
            setInvoiceId.add(triggerItem.Invoice__c);
    }
    
    // Sum the total cost of the line items
    // for the current invoice (see invoice above).
    //
    // 1. Get all of the line items for the invoice.
    // 2. For each line item update the mapping for the invoice 
    //    to be (previous total + total cost).    
    List<Line_Item__c> lstInvLineItem_LineItemTotal = new List<Line_Item__c>();
    List<Line_Item__c> lstInvLineItem_LineItemTotalAdjusted = new List<Line_Item__c>();
    List<Line_Item__c> lstInvLineItem_NumberofLineItems = new List<Line_Item__c> ();
    if(setInvoiceId.size()>0){
        lstInvLineItem_LineItemTotal = [SELECT Id, Cost__c, Invoice__c FROM Line_Item__c
                WHERE Invoice__c IN :setInvoiceId];
        lstInvLineItem_LineItemTotalAdjusted = [SELECT Id, Total_Cost_incl_Adjustment__c, Invoice__c FROM Line_Item__c
                WHERE Invoice__c IN :setInvoiceId];
        lstInvLineItem_NumberofLineItems = [SELECT Id, One__c, Invoice__c FROM Line_Item__c
                WHERE Invoice__c IN :setInvoiceId];
    }
    // If the list has records, add those IDs and totals to the map.
    if(lstInvLineItem_LineItemTotal.size()>0){
        for(Line_Item__c item:lstInvLineItem_LineItemTotal){    
            LineItemTotal.put(item.Invoice__c, 
                LineItemTotal.get(item.Invoice__c) + item.Cost__c); 
        }
    }
    // If the list has records, add those IDs and totals to the map.    
    if(lstInvLineItem_LineItemTotalAdjusted.size()>0){
        for(Line_Item__c item:lstInvLineItem_LineItemTotalAdjusted){    
            LineItemTotalAdjusted.put(item.Invoice__c, 
                LineItemTotalAdjusted.get(item.Invoice__c) + item.Total_Cost_incl_Adjustment__c); 
        }
    }
    
    // If the list has records, add those IDs and totals to the map.
    if(lstInvLineItem_NumberofLineItems.size()>0) {
        for(Line_Item__c item:lstInvLineItem_NumberofLineItems) {
            NumberofLineItems.put(item.Invoice__c,
                NumberofLineItems.get(item.Invoice__c) + item.One__c);
        }
    }
    
    List<Id> invoices = new List<Id>();
    for (Id previous : LineItemTotal.keyset()) {
        invoices.add(previous);
    }
    for (Id current : LineItemTotal.keyset()) {
        if (!LineItemTotal.containsKey(current)) {
            invoices.add(current);
        }
    }
    // Get a list of all invoices where its Id is in the invoices map.
    List<Invoice__c> lstInvoices = new List<Invoice__c>();
    if(invoices.size()>0){
        lstInvoices = [SELECT Id, Line_Item_Total__c, Line_Item_Total_Adjusted__c, Number_of_Line_Items__c
             FROM Invoice__c WHERE Id IN :invoices];
    }
    // Update the map with line item and line item adjusted totals.
    if(lstInvoices.size()>0){
        for(Invoice__c invoice:lstInvoices){
            if (LineItemTotal.containsKey(invoice.id)) {
                invoice.Line_Item_Total__c = LineItemTotal.get(invoice.id);
            }
            
            if (LineItemTotalAdjusted.containsKey(invoice.id)) {
                invoice.Line_Item_Total_Adjusted__c = LineItemTotalAdjusted.get(invoice.id);
            }
            
            if (NumberofLineItems.containsKey(invoice.id)) {
                invoice.Number_of_Line_Items__c = NumberofLineItems.get(invoice.id);
            }
            
            invoicesToUpdate.add(invoice);
        }
    }
    update invoicesToUpdate;
}


Can someone help me figure out what's wrong with this trigger? I'm writing it to create lookup rollup-summary esque fields on the parent object, Invoice__c, from the child Line_Item__c. I get this error though upon updating a record:

"Line_Item_Invoice_RollupTrigger: execution of AfterUpdate
caused by: System.NullPointerException: Argument cannot be null.
Trigger.Line_Item_Invoice_RollupTrigger: line 55, column 1"

Here's the trigger:
trigger Line_Item_Invoice_RollupTrigger on Line_Item__c (after update, after insert, after delete) {
     Map<Id, Double> LineItemTotal = new Map<Id, Double>();
     Map<Id, Double> LineItemTotalAdjusted = new Map<Id, Double>();
     Map<Id, Double> NumberofLineItems = new Map<Id, Double>();
     List<Invoice__c> invoicesToUpdate = new List<Invoice__c>();
     List<Line_Item__c> itemsForInvoice;
     List<Line_Item__c> items;
     if (trigger.isDelete) {
         items = trigger.old;
     }
     else {
         items = trigger.new;
    }
    // Go through all line items that the trigger is acting on.
    Set<ID> setInvoiceId = new Set<ID>();
    for (Line_Item__c triggerItem: items) {       
        
            // Get the invoice's ID for the line item and then
            // ensure its ID exists in the line item / line item adjusted totals
            // maps and their totals default to $0.00.        
        if (!LineItemTotal.containsKey(triggerItem.Invoice__c)) {
            LineItemTotal.put(triggerItem.Invoice__c, 0.0);
        }
        if (!LineItemTotalAdjusted.containsKey(triggerItem.Invoice__c)) {
            LineItemTotalAdjusted.put(triggerItem.Invoice__c, 0.0);
        }
        if (!NumberofLineItems.containsKey(triggerItem.Invoice__c)) {
            NumberofLineItems.put(triggerItem.Invoice__c, 0);
        }
        
        if(triggerItem.Invoice__c != NULL)
            setInvoiceId.add(triggerItem.Invoice__c);
    }
    
    // Sum the total cost of the line items
    // for the current invoice (see invoice above).
    //
    // 1. Get all of the line items for the invoice.
    // 2. For each line item update the mapping for the invoice 
    //    to be (previous total + total cost).    
    List<Line_Item__c> lstInvLineItem_LineItemTotal = new List<Line_Item__c>();
    List<Line_Item__c> lstInvLineItem_LineItemTotalAdjusted = new List<Line_Item__c>();
    List<Line_Item__c> lstInvLineItem_NumberofLineItems = new List<Line_Item__c> ();
    if(setInvoiceId.size()>0){
        lstInvLineItem_LineItemTotal = [SELECT Id, Cost__c, Invoice__c FROM Line_Item__c
                WHERE Invoice__c IN :setInvoiceId];
        lstInvLineItem_LineItemTotalAdjusted = [SELECT Id, Total_Cost_incl_Adjustment__c, Invoice__c FROM Line_Item__c
                WHERE Invoice__c IN :setInvoiceId];
        lstInvLineItem_NumberofLineItems = [SELECT Id, One__c, Invoice__c FROM Line_Item__c
                WHERE Invoice__c IN :setInvoiceId];
    }
    // If the list has records, add those IDs and totals to the map.
    if(lstInvLineItem_LineItemTotal.size()>0){
        for(Line_Item__c item:lstInvLineItem_LineItemTotal){    
            LineItemTotal.put(item.Invoice__c, 
                LineItemTotal.get(item.Invoice__c) + item.Cost__c); 
        }
    }
    // If the list has records, add those IDs and totals to the map.    
    if(lstInvLineItem_LineItemTotalAdjusted.size()>0){
        for(Line_Item__c item:lstInvLineItem_LineItemTotalAdjusted){    
            LineItemTotalAdjusted.put(item.Invoice__c, 
                LineItemTotalAdjusted.get(item.Invoice__c) + item.Total_Cost_incl_Adjustment__c); 
        }
    }
    
    // If the list has records, add those IDs and totals to the map.
    if(lstInvLineItem_NumberofLineItems.size()>0) {
        for(Line_Item__c item:lstInvLineItem_NumberofLineItems) {
            NumberofLineItems.put(item.Invoice__c,
                NumberofLineItems.get(item.Invoice__c) + item.One__c);
        }
    }
    
    List<Id> invoices = new List<Id>();
    for (Id previous : LineItemTotal.keyset()) {
        invoices.add(previous);
    }
    for (Id current : LineItemTotal.keyset()) {
        if (!LineItemTotal.containsKey(current)) {
            invoices.add(current);
        }
    }
    // Get a list of all invoices where its Id is in the invoices map.
    List<Invoice__c> lstInvoices = new List<Invoice__c>();
    if(invoices.size()>0){
        lstInvoices = [SELECT Id, Line_Item_Total__c, Line_Item_Total_Adjusted__c, Number_of_Line_Items__c
             FROM Invoice__c WHERE Id IN :invoices];
    }
    // Update the map with line item and line item adjusted totals.
    if(lstInvoices.size()>0){
        for(Invoice__c invoice:lstInvoices){
            if (LineItemTotal.containsKey(invoice.id)) {
                invoice.Line_Item_Total__c = LineItemTotal.get(invoice.id);
            }
            
            if (LineItemTotalAdjusted.containsKey(invoice.id)) {
                invoice.Line_Item_Total_Adjusted__c = LineItemTotalAdjusted.get(invoice.id);
            }
            
            if (NumberofLineItems.containsKey(invoice.id)) {
                invoice.Number_of_Line_Items__c = NumberofLineItems.get(invoice.id);
            }
            
            invoicesToUpdate.add(invoice);
        }
    }
    update invoicesToUpdate;
}