You need to sign in to do that
Don't have an account?
Jason Walstad 2
SELF_REFERENCE_FROM_TRIGGER Error
Hi all,
I am working on a trigger on the Contract object which creates Events and Orders associated with the activation of a contract. My trigger is throwing an error when I try to assign the ContractId to my newly created Order object. The error does not occur when I assign the ContractId to my Event object, however. Any tips on how to work around this?
Here are some code snippets (Note that 'temp' is a local variable of type Contract):
This code generates the error:
Here is the error generated:
Thank you.
I am working on a trigger on the Contract object which creates Events and Orders associated with the activation of a contract. My trigger is throwing an error when I try to assign the ContractId to my newly created Order object. The error does not occur when I assign the ContractId to my Event object, however. Any tips on how to work around this?
Here are some code snippets (Note that 'temp' is a local variable of type Contract):
This code generates the error:
Order o1 = new Order(AccountId = temp.AccountId, EffectiveDate = temp.Requested_First_Service__c, ContractId = temp.Id, <---PROBLEM CODE Status = 'Draft');Accompanying Event creation code, this does not generate an error:
Event e1 = new Event(WhatId = temp.AccountId, AccountOrSite__c = temp.AccountId, Contract__c = temp.Id, StartDateTime = temp.Requested_First_Service__c, EndDateTime = te mp.Requested_First_Service__c + 7, Subject = 'Initial Followup');
Here is the error generated:
Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger StaggeredEventCreation caused an unexpected exception, contact your administrator: StaggeredEventCreation: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 8003B000000DDyx) is currently in trigger StaggeredEventCreation, therefore it cannot recursively update itself: []: Trigger.StaggeredEventCreation: line 67, column 1".Any tips?
Thank you.
All Answers
I can't be certain based on the code snippets you provided, but I'm guessing later in your code you have
By doing so, and since o1 is related to "temp", a Contract in trigger.new, you're "updating" temp. This results in the error message you're receiving, which is basically saying you can't update an item in the before trigger since it would cause an endless recursion of the trigger.
Try putting this in the after update context, and adding in criteria for each Contract in trigger.new to know if a new Order needs to be created for it or not (to prevent recursion).
Thanks for the response. I'm not fully following what you are referring to in your solution though. What should I try putting in the after update context? I'm going to attach the full trigger to help clarify things on my end.