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
Jim MontgomeryJim Montgomery 

maximum trigger depth error

I have a trigger on task that will update the task with the accounts record type on insertion.
I also need to do this on an update, but get the "maximum Trigger Depth" error when adding before update to the trigger.

trigger TaskAccountType on Task (after insert) {
    Task T = [select Id,WhatId,subject from task where Id = :trigger.new[0].Id];
    if(trigger.new[0].Id!=null){
    string WhatId = t.WhatId;
    
    if(t.WhatId!=NULL){
   if(WhatId.startswith('0')){
    Account a = [select id,recordtypeid from Account where id =: trigger.new[0].AccountId];
if(trigger.new[0].AccountId!=null){  
if(a.recordtypeid == '012i0000000Ou0CAAS'){
        t.Account_record_Type__c ='Customer Account'; 
        update T;       
        }
        else
        if(a.recordtypeid == '012i0000000Ou0EAAS'){
        t.Account_record_Type__c = 'Prospect Account';  
        update T;      
        }
        else
        if(a.recordtypeid == '012i0000000Ou0DAAS'){
        t.Account_record_Type__c = 'Partner Account';  
        update T;      
        }
}
}
}
}
}
Best Answer chosen by Jim Montgomery
Steven NsubugaSteven Nsubuga
This is due to the "infinite" loop you set up!!  Inside your Task trigger, you update the Trigger, causing the trigger to fire again, and again, and again ...

All Answers

Steven NsubugaSteven Nsubuga
This is due to the "infinite" loop you set up!!  Inside your Task trigger, you update the Trigger, causing the trigger to fire again, and again, and again ...
This was selected as the best answer
Jim MontgomeryJim Montgomery
OK, I have made the following code changes. Works fine on update, but not on insert. Record Type is not stamped on the activity.


trigger TaskAccountTypeContact on Task (before insert, before update) {
   For (Task T : trigger.new){            
            string WhoID = t.WhoID;
    if(t.WhoId!=NULL){
   if(WhoID.startswith('0')){
   if(T.AccountID!=NULL){
    Account a = [select id,recordtypeid from Account where id =:T.AccountId]; 
if(a.recordtypeid == '012i0000000Ou0CAAS'){
        t.Account_record_Type__c ='Customer Account';                
        }
        else
        if(a.recordtypeid == '012i0000000Ou0EAAS'){
        t.Account_record_Type__c = 'Prospect Account';                
        }
        else
        if(a.recordtypeid == '012i0000000Ou0DAAS'){
        t.Account_record_Type__c = 'Partner Account';               
        }
}
}
}
}
}