function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matt EmaraMatt Emara 

Trigger to Create Record After Field Update

Hi All,

I am writing a trigger that creates an Invoice Item based on a selection of services.

The trigger works well if the selection is made on record creation but if it is being updated it doesn't fire.

Here is the code:

trigger PickupService on Invoice__c (after Insert) {
    List <Invoice_Item__c> Item = new List <Invoice_Item__c>();
    for (Invoice__c Inv: Trigger.new)
    {
        if (Inv.Pickup_Service__c == True)
        {
            Invoice_Item__c I = new Invoice_Item__c();
            I.Invoice__c = Inv.Id;
            I.Quantity__c= 1;
            I.Product__c = '01t4B000000Rnir';
            I.Service_Fee_Type__c = 'Room Service- Pickup';
            I.Item_Price__c = 15.00;
            
            Item.add(I);           
            
        }
        insert Item;
    
    }
}

When I add 'after update' the trigger fails to fire and the original record fails to create. I've tried doing this using Process Builder as well and the same issue occurs.

The error I email I get is very long but the final error states: CUMULATIVE_LIMIT_USAGE_END
There are other Apex Classes and Triggers build into the org is it possible that because of the existing code there are too many queries being processed?

I've tried building the same functionality in a different Sandbox that is blank and it is working fine. Any ideas?

Thanks for your help.
Deepak GulianDeepak Gulian
You have placed insert item inside for loop, I just fixed that now try this below code   
trigger PickupService on Invoice__c (after Insert, after update) {
    List <Invoice_Item__c> Item = new List <Invoice_Item__c>();
    for (Invoice__c Inv: Trigger.new)
    {
        if (Inv.Pickup_Service__c == True)
        {
            Invoice_Item__c I = new Invoice_Item__c();
            I.Invoice__c = Inv.Id;
            I.Quantity__c= 1;
            I.Product__c = '01t4B000000Rnir';
            I.Service_Fee_Type__c = 'Room Service- Pickup';
            I.Item_Price__c = 15.00;
            
            Item.add(I);           
            
        }
       
    }
    insert Item;
}
Matt EmaraMatt Emara
Hi Deepak,

Thanks for your response.

I've tried adjusting the code like that previously and still the same issue occurs.

Thanks
Deepak GulianDeepak Gulian
CUMULATIVE_LIMIT_USAGE is not an error, it's just a part of the debug log that indicates how close you are to various limits.
So you should check out the debug logs and get to know which limit is about to reach out.
pigginsbpigginsb
It seems like there might be an update to Invoice after Invoice Items are added, causing this trigger to fire again on update. If this is the case, then you'd want to make sure you are only creating the necessary Invoice item records once per invoice.
Amit Chaudhary 8Amit Chaudhary 8
I found some issue in below trigger. Please check my comment inline :-

trigger PickupService on Invoice__c ( after Insert , after update )
{
    List <Invoice_Item__c> Item = new List <Invoice_Item__c>();
    for (Invoice__c Inv: Trigger.new)
    {
        if (Inv.Pickup_Service__c == True)
        {
            Invoice_Item__c I = new Invoice_Item__c();
            I.Invoice__c = Inv.Id;
            I.Quantity__c= 1;
            I.Product__c = '01t4B000000Rnir'; // Please never use Hard Code ID
            I.Service_Fee_Type__c = 'Room Service- Pickup';
            I.Item_Price__c = 15.00;
            
            Item.add(I);           
            
        }
    }
    if(Item.size() > 0 )
    {
        insert Item; // Always check size of list before DML
    }
}


The debug log will contain the Cumulative resource usage (the LIMIT_USAGE_FOR_NS event identifer) when a code unit has finished. Note: it will log this information after many code units are finished

Recommended Reading:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_debugging_debug_log.htm