0

i have written a trigger to create 3 reocrds Opportunity,Case and Task when Account Status is equal to New.But my trigger is working while insert,Not in Update.

Here is My Code:

trigger CreateTaskcaseandOpp on Account (After Insert,After Update)
{
List<Task> tasks = new List<Task>();
List<Opportunity> opp = New List<Opportunity>();
List<Case> Cases = New List<Case>();
for (Account acc : Trigger.New)
{
    if(acc.Status__c=='New')
    {
        Task tsk = new Task(whatID = acc.ID); 
        tasks.add(tsk);
        Opportunity o = New Opportunity(AccountId=acc.id,Name=acc.Name,CloseDate=System.Today(),StageName='Closed Won');
        opp.add(o);
        Case c = New Case(AccountId=acc.Id);
        Cases.add(c);
    } 
}
    insert tasks;
    insert opp;
    Insert Cases;
}

I am Facing This Type of Error enter image description here

Can Any one Help Me,What mistake i have done ??

Thanks

1
  • 1
    It's getting stuck in a loop. For example: an update to an Opportunity might do an update an Account, then an Account might do an update to an Opportunity which is then doing an update to an Account etc... It will be worth checking Triggers on your Tasks, Opportunity and Cases objects.
    – Dan Jones
    Jul 3, 2017 at 16:19

2 Answers 2

3

You have some other configuration (process builder, workflow, etc) that's causing an update to the account, which causes new records to be created, which causes an update to the account, which causes new records to be created... In other words, you have an infinite recursion problem. You'll probably want to create a class that helps prevent this recursion. The preferred pattern today looks like this:

public class RecursionHelper {
  static Set<Id> recordIds = new Set<Id>();
  public static Boolean runOnce(Set<Id> newIds) {
    return recordIds.addAll(newIds);
  }
}

Then, in your trigger:

if(RecursionHelper.runOnce(Trigger.newMap.keySet()) {
  // Implement your logic here
}
3
  • Keeping in mind it is not reset between chunks if record count is > 200. That is still the case right?
    – Eric
    Jul 3, 2017 at 16:43
  • @Eric That's why using a Set is preferred; this makes sure that multiple chunks will be correctly handled.
    – sfdcfox
    Jul 3, 2017 at 16:45
  • Doh. That's what I get for quickly scanning on mobile. Nice
    – Eric
    Jul 3, 2017 at 16:46
0

By introducing a static bool variable in your trigger can help you to avoid this problem.

This error comes up when salesforce try to update a record which is just already updated and this recursive call ends up on the error you facing.

First of all use atleast the simplest pattern which differentiate between the calls by using

trigger.isInsert

trigger.isUpdate

and then use this technique to prevent the maximum depth exceeded error.

Class for Trigger Handler: 

public class AccountHandlerController{
        if(TriggersUtil.check){
           TriggersUtil.check = false;
           public static perform(List<Account> listAccount){
           for(Account acct : listAccount){
              //your code goes here;
           }
        update listAccount;
       }
    }
}

Class for storing boolean variable

public class TriggersUtil{
        public static Boolean check = true;
}

You must log in to answer this question.