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
Jason Walstad 2Jason 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:
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.


 
Best Answer chosen by Jason Walstad 2
matt.ian.thomasmatt.ian.thomas
Sorry for being unclear. Change before update to after update and see if that works.

All Answers

matt.ian.thomasmatt.ian.thomas
Hey Jason,

I can't be certain based on the code snippets you provided, but I'm guessing later in your code you have 
insert o1;

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).
Jason Walstad 2Jason Walstad 2
Hi Matt,

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. 
 
trigger StaggeredEventCreation on Contract(before update){
    
    List<Contract> c = Trigger.new;
    List<Event> eventList = new List<Event>();
    List<Order> orderList = new List<Order>();
    
    for (Contract temp : c){
        if (temp.Status == 'Activated' && temp.Status != Trigger.oldMap.get(temp.ID).Status){
            
            //new Events associated w/ the Contract
            Event e1 = new Event(OwnerId = '00561000000kWZHAA2', WhatId = temp.AccountId, AccountOrSite__c = temp.AccountId, 
                                 Contract__c = temp.Id,
                                 StartDateTime = temp.Requested_First_Service__c, EndDateTime = temp.Requested_First_Service__c + 7, 
                                 Subject = 'Initial Followup');
            eventList.add(e1);
            
            //new Orders associated w/ the Contract
            Order o1 = new Order(AccountId = temp.AccountId, EffectiveDate = temp.Requested_First_Service__c, 
                                 ContractId = temp.Id,
                                 Status = 'Draft');
             orderList.add(o1);
        }
    }
    
    if(eventList.size() > 0)
		insert eventList;
    
    if(orderList.size() > 0)
		insert orderList;

 
matt.ian.thomasmatt.ian.thomas
Sorry for being unclear. Change before update to after update and see if that works.
This was selected as the best answer
Jason Walstad 2Jason Walstad 2
Wow, it was that simple. Thanks for the help!