0

I am getting bit confused on the probable answer as to what would happen on executing the below code block (considering trigger event as After Insert and scenario as to Create a Follow Up Task whenever a new Task is created)?

List<Task> newLstTask = new List<Task>();
    for(Task t : trigger.new){
      Task tsk = new Task();
      tsk.Subject = 'Follow Up';
      newLstTask.add(tsk);
    }
insert newLstTask;

Would this as I'm thinking result in infinite loop and eventually fail or would it end up creating multiple tasks for each Task in trigger.new?

Currently I get an error dml statement cannot operate on trigger.new or trigger.old after insert when I create a Task from UI on any object.

Please see below the error when I create a Task on UI: enter image description here

2

This trigger would eventually fail when the maximum trigger recursion depth was reached, which is 16. This means that, assuming there was 1 task to begin with, an additional 15 tasks would be created, and finally the system would throw a System.LimitException (which cannot be caught and is immediately fatal to the current transaction).

If you have an error regarding "DML statement cannot operate on Trigger.new or Trigger.old", this would be in a separate trigger or line of code somewhere. The code provided in your question would not result in this error by itself. You would need to do additional debugging to find out where this error is coming from.

  • Thanks for the explanation but for the second paragraph i am unable to understand the logic behind the DML statement error.There is only one trigger for the task object and if i deactivate the trigger i am not encountering the error. Could you please help me understand! - Thanks. – Sound of Silence Jun 5 at 12:29
  • @SoundofSilence Re: the second paragraph: The code you've posted can't generate the error you've specified. Your trigger must have more code than what you've posted, OR you've posted the wrong code. Try copying the code in your question directly in to a new developer org, and you'll see the error I mentioned about the LimitException. – sfdcfox Jun 5 at 12:41
  • I did try this out on my dev org. Have provided the error image above on the question. – Sound of Silence Jun 5 at 13:07
  • My Bad I tried this on a different dev org and I can see the maximum trigger depth exceeded exception coming now ! As you said there would be something else that was causing the exception dml statement cannot operate on trigger.new or trigger.old -Thanks! – Sound of Silence Jun 5 at 13:17
  • 1
    @SoundofSilence Your screenshot strongly suggests this is a "custom object", and not a Task. The color is wrong for the Task object (Tasks are a blueish tint, this object has a greenish tint), the "Owner" field should be "Assigned To", and it does not have the correct buttons (Save & New Task, Save & New Event). You'll want to check that object's triggers for the code causing the problem. – sfdcfox Jun 5 at 13:21
0

It seems the error is coming from somewhere else than above code. Possibly a trigger depth exceeding error could be avoided adding some logic similar to below (to avoid newly created tasks to run trigger again to create another set of tasks, hence recursively end up in max. trigger depth exception).

List<Task> newLstTask = new List<Task>();
    for(Task t : trigger.new){
      if (t.subject!='Follow Up'){
         Task tsk = new Task();
         tsk.Subject = 'Follow Up';
         newLstTask.add(tsk);
      }
    }
insert newLstTask;

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.