You need to sign in to do that
Don't have an account?

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.
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.
Thanks for your response.
I've tried adjusting the code like that previously and still the same issue occurs.
Thanks
So you should check out the debug logs and get to know which limit is about to reach out.
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