You need to sign in to do that
Don't have an account?
GMASJ
Self Recursive error in trigger updating a string field based on date
Hi,
Please suggest me what is the mistake in the below code.
I receive below error
Sudhir
Please suggest me what is the mistake in the below code.
trigger CaptureStatusTimeZone on Task (After Insert,Before Update) { String timeZone; Datetime dateGMT; Datetime d1; Set<Id> tid = new Set<Id>(); for(Task T : Trigger.new){ tid.add(t.id); } List<Task> updatetask = new List<Task>(); updatetask = [Select Id,status,createddate,createdbyid,lastmodifieddate,lastmodifiedbyid, lastmodifiedby.TimeZoneSidKey from task where Id in :tid]; List<Task> updatetasks = new List<Task>(); for(Task ts : updatetask){ if(ts.Status == 'Completed') { dateGMT = ts.lastmodifieddate; d1 = Datetime.valueOf(dateGMT); ts.Status_Completed_Time_Zone_Date__c = d1.format('MM/dd/yyyy HH:mm:ss',ts.lastmodifiedby.TimeZoneSidKey); updatetasks.add(ts); } } update updatetasks; }
I receive below error
Error: Invalid Data. Review all error messages below to correct your data. Apex trigger CaptureStatusTimeZone caused an unexpected exception, contact your administrator: CaptureStatusTimeZone: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00T1b000002yhaZEAQ; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00T1b000002yhaZ) is currently in trigger CaptureStatusTimeZone, therefore it cannot recursively update itself: []: Trigger.CaptureStatusTimeZone: line 29, column 1Thanks
Sudhir
The trigger code tries to update same record in before update. We need not have explicit update statement. Below is the code sample without considering timezone. I have also replaced after insert into before insert since before insert should be fine to assign value for the same record
trigger CaptureStatusTimeZone on Task (before Insert,Before Update) {
for(Task ts : Trigger.new){
if(ts.Status == 'Completed' && (Trigger.isInsert || trigger.oldMap.get(ts.id).Status != ts.status))
{
ts.Status_Completed_Time_Zone_Date__c = System.Now().format('MM/dd/yyyy HH:mm:ss');
}
}
}