+ Start a Discussion
chandan kumar 99chandan kumar 99 

SELF_REFERENCE_FROM_TRIGGER

Hi Gurus, 

I have a trigger on Task object and I am trying to before insert and update a field Task object. I am getting following error message:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger WeekendTasks caused an unexpected exception, contact your administrator: WeekendTasks: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00T7F00000G5ulOUAR; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00T7F00000G5ulO) is currently in trigger WeekendTasks, therefore it cannot recursively update itself: []: Trigger.WeekendTasks: line 15, column 1

Here is my trigger
trigger WeekendTasks  on Task (before insert,Before update) {
    list<Task> Tasweek =new List<Task>();
           for(Task tweek:Trigger.new){
                if(tweek.Subject != null) {
                   Date origin = Date.newInstance(1900,1,6);
                   Date due = tweek.ActivityDate;
                   Integer x = origin.daysBetween(due);
                   Integer day = Math.mod(x,7);
                   if (day < 2 ) {
                     Task tas = new Task (Id = tweek.Id,ActivityDate = (tweek.ActivityDate + 2));
                     Tasweek.add(tas);
                   }
                 }
           }
          update Tasweek;  
}
Best Answer chosen by chandan kumar 99
Arpit Jain7Arpit Jain7
Hello Chandan,

Try below code

trigger WeekendTasks  on Task (before insert,Before update) {
    list<Task> Tasweek =new List<Task>();
           for(Task tweek:Trigger.new){
                if(tweek.Subject != null) {
                   Date origin = Date.newInstance(1900,1,6);
                   Date due = tweek.ActivityDate;
                   Integer x = origin.daysBetween(due);
                   Integer day = Math.mod(x,7);
                   if (day < 2 ) {
                     tweek.ActivityDate = tweek.ActivityDate + 2;
                   }
                 }
           }          
}

Thanks
Arpit

All Answers

Arpit Jain7Arpit Jain7
Hello Chandan,

Try below code

trigger WeekendTasks  on Task (before insert,Before update) {
    list<Task> Tasweek =new List<Task>();
           for(Task tweek:Trigger.new){
                if(tweek.Subject != null) {
                   Date origin = Date.newInstance(1900,1,6);
                   Date due = tweek.ActivityDate;
                   Integer x = origin.daysBetween(due);
                   Integer day = Math.mod(x,7);
                   if (day < 2 ) {
                     tweek.ActivityDate = tweek.ActivityDate + 2;
                   }
                 }
           }          
}

Thanks
Arpit
This was selected as the best answer
chandan kumar 99chandan kumar 99
@Arpit Jain 

Thanks this code is running but
 list <task> Taskweek = new List<Task>();
no any data store on task so overall Trigger is running fine