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
KitagawaSan1337KitagawaSan1337 

error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, maximum trigger depth exceeded

I have a trigger to update a custom 'last activity date' when ever a task is marked as closed: 

 

trigger TaskUpdateAccountdate on Task (after insert, after update) {

List<Account> acctToUpdate = new List<Account>();

for(Task tsk : trigger.new)
{if (tsk.IsClosed)
accsToUpdate.add(new Account(Id = tsk.AccountId, Last_Activity_Date__c = Datetime.now()));
}

Update acctToUpdate;
}

 

It was working before, but now I get an error: 

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger TaskUpdateAccountdate caused an unexpected exception, contact your administrator: TaskUpdateAccountdate: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001R000000eOkIEIA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateOpenTaskOwner: maximum trigger depth exceeded Task trigger event AfterUpdate for [00TR000000CVtFR] Account trigger event AfterUpdate for [001R000000eOkIE] Task trigger event AfterUpdate for [00TR000000CVtFR] Account trigger event AfterUpdate for [001R000000eOkIE] Task trigger event AfterUpdate for [00TR000000CVtFR] Account trigger event AfterUpdate for [001R000000eOkIE] Task trigger event AfterUpdate for [00TR000000CVtFR] Account trigger event AfterUpdate for [001R000000eOkIE] Task trigger event AfterUpdate for [00TR000000CVtFR] Account trigger event AfterUpdate for [001R000000eOkIE] Task trigger event AfterUpdate for [00TR000000CVtFR] Account trigger event AfterUpdate for [001R000000eOkIE] Task trigger event AfterUpdate for [00TR000000CVtFR] Account trigger event AfterUpdate for [001R000000eOkIE] Task trigger event AfterUpdate for [00TR000000CVtFR] Account trigger event AfterUpdate for [001R000000eOkIE]: []: Trigger.TaskUpdateAccountdate: line 10, column 1

 

Not sure what is going on, or why this worked before... 

 

Any ideas? 

 

Thanks!

 

SuperfellSuperfell

It looks like you have this trigger on Task updating accounts, and a trigger on accounts updating tasks, and so they end up goind round & round between them until you get to the trigger depth limit (this is why there's a trigger depth limit). one easy fix would be to look from the transition on isClosed (i.e. trigger.new[i].isClosed && !trigger.old[i].isClosed rather than just looking at the current state, which'll fire every time.

KitagawaSan1337KitagawaSan1337

Ah, youre right... 

 

I was trying to write a trigger to transfers all open tasks for an account when the account owner is changed... That must have caused the issue. Is there a way to write such a trigger that would not cause this issue? It seems like it would be fairly easy, but I am admittedly not a dev... 

 

 

 

 

Andy BoettcherAndy Boettcher

Check out this article from the cookbook on preventing Trigger recursion:

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

 

-Andy

KitagawaSan1337KitagawaSan1337

Thanks!

 

I got this so far, but am still getting an error: 
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Task> at line 6 column 5 

 

trigger UpdateTasks on Account (after update)
{
    Set<Id> ids=Trigger.newmap.keyset();
    List<Task> t =[SELECT ID, WhatId,Status,OwnerId FROM task WHERE WhatId IN:ids];
    for(Task ol: t)
    t.Status='Completed';
   update t;
}

 

Andy BoettcherAndy Boettcher

Looks like you're flipping around your variables - within the loop, you need to reference "ol" as the Task record.  "t" at the end is your list.

 

    Set<Id> ids=Trigger.newmap.keyset();
    List<Task> t =[SELECT ID, WhatId,Status,OwnerId FROM task WHERE WhatId IN:ids];
    for(Task ol: t)
    ol.Status='Completed';
   update t;
}

 

lakshmi salesforcelakshmi salesforce
hi every one.....

i am new to triggers  i have a requirement of creating one new record in child custom object(job application) when custom master object (position) have new or updated the records with max pay greater than 1000000 i am getting this error while i am updating an existing record in such way that it is meeting the criteria as


Error:Apex trigger newJobAppCreationForCriticalPos caused an unexpected exception, contact your administrator: newJobAppCreationForCriticalPos: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, newJobAppCreationForCriticalPos: maximum trigger depth exceeded position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee] position trigger event AfterUpdate for [a0328000008xKee]: []: Trigger.newJobAppCreationForCriticalPos: line 12, column 1


my apex trigger code is


trigger newJobAppCreationForCriticalPos on position__c (after update,after insert) {
    list<Job_Application__c> jobAppRecList=new list<Job_Application__c>();
    for(position__c posRec:trigger.new){
        if(posRec.Max_Pay__c>1000000){
            Job_Application__c jobAppRec=new Job_Application__c();
            jobAppRec.position__c=posRec.id;
            jobAppRec.Status__c='open';
            jobAppRec.Stage__c='new';
            //insert jobAppRec;
            jobAppRecList.add(jobAppRec);
        }
    }insert jobAppRecList;

}

anybody please help me on this
 
Priyanka Phatangare 7Priyanka Phatangare 7
Hi Everyone...

 I have to update record on same object by using after update event.. I'm getting this error while I'm updating an existing record in such way.
My trigger logic is

Trigger accountTrigger on Account (after Update) {
 Account myAccount=trigger.new[0];
    //requery on the record to create a copy of the record
    Account updateaccount=[select id from account where id = :myAccount.id];
    
    updateaccount.type='Prospect';
    update updateaccount;

}
User-added image

Please help me on this.
Thanks !