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
TanyrilTanyril 

Trigger stopped working after 2 months in production- still works on sandbox HELP!

Here's the trigger:

trigger HistoricalAUM on Account (after insert, before update) {

List<Historical_AUM__c> TransferAUM = new List <Historical_AUM__c> {};

 Account[] accts;
    if (Trigger.isDelete) 
        accts = Trigger.old;
    else
        accts = Trigger.new;

for (account a : accts) {

TransferAUM.add( new Historical_AUM__c(Year_Revenue__c = a.Revenue__c, Revenue__c = a.Month_Revenue__c));

}
try {
upsert TransferAUM UpsertID__c;
}
catch (Exception Ex)
{
system.debug(Ex);
}
}

 

Here's the problem, but ONLY in production:

06:59:09.181 (181633000)|EXCEPTION_THROWN|[17]|System.DmlException: Upsert failed. First exception on row 0; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 001d000000CluwJ) is currently in trigger HistoricalAUM, therefore it cannot recursively update itself: []

Test data works as expected in the sandbox, and had worked up until sometime in the last few weeks in production.

Best Answer chosen by Admin (Salesforce Developers) 
TanyrilTanyril

I changed the trigger to fire AFTER update which solved the problem. I'm curious as to why it had been working for the last two months with BEFORE update, but C'est La vie...

All Answers

Vinit_KumarVinit_Kumar

The problem is with this line :-

 

upsert TransferAUM UpsertID__c;

 

If there is any existing record,it would try to update it.However,the Trigger is on Before Update and you can't explicitly perform update in before Trigger as it is done implicitly.

 

I hope you have got my point.

TanyrilTanyril

The trigger is creating a new instance of a different object which already exists (or updating the existing instance- situationally).

"Before Update" just captures the state before the change to the account is committed and transfers it to another object.

 

As I stated previously: The code works in sandbox and WAS working in production. If your suggestion were correct it should have never worked.

 

Vinit_KumarVinit_Kumar

Not sure what you mean what I want to say if it is before update you should not explicitly update it.

 

The way I would approach it is by checking the debug logs and see what is going on here.

TanyrilTanyril

I'm not updating the account object. I'm reading fields from ACCOUNT and writing them to Historical_AUM__C before an update to ACCOUNT is commited. - Essentially creating a back up copy of the data that was in the account object before someone changed it.


I provided the error from the debug log in my first post, but this is what it is:

 

06:59:09.181 (181633000)|EXCEPTION_THROWN|[17]|System.DmlException: Upsert failed. First exception on row 0; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 001d000000CluwJ) is currently in trigger HistoricalAUM, therefore it cannot recursively update itself: []

TanyrilTanyril

I changed the trigger to fire AFTER update which solved the problem. I'm curious as to why it had been working for the last two months with BEFORE update, but C'est La vie...

This was selected as the best answer
Vinit_KumarVinit_Kumar

What I would assume here is that ,the Trigger is running on insert event till now,not trying to update anything.The day it started to update as part of upsert operation,it started to fail.

 

However,all is well that ends well,good to know it is resolved now :)

TanyrilTanyril

We had tested updates... and it was working for updates. Either way, problem solved. Thanks for your help!