You need to sign in to do that
Don't have an account?
Recursive Update Error on connected Opportunity/Task Triggers
Hello all,
I am having an issue with two triggers I have written. One trigger creates a task of a certain recordtype whenever a field on an Opportunity is set to a certain value, while the second trigger assigns some values from this task back to the related opportunity upon update to specific Task fields.
This should work without issue, in theory anyway, but it seems as though the two triggers are acting simultaneously (though they should not be).
The error I get is:
Error:Apex trigger assignUnscheduledTask caused an unexpected exception, contact your administrator: assignUnscheduledTask: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateUnschedOpp: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006K0000003dWJPIA2; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 006K0000003dWJP) is currently in trigger assignUnscheduledTask, therefore it cannot recursively update itself: [] Trigger.UpdateUnschedOpp: line 19, column 1: []: Trigger.assignUnscheduledTask: line 25, column 1
The initial trigger is :
trigger assignUnscheduledTask on Opportunity (before update, before insert) { public Task tsk {get; set;} Recordtype RT = [select Id, Name from RecordType where sObjectType = 'Task' AND Name = 'Un-Scheduled Deal Task']; for (Opportunity o : trigger.new) { system.debug('CREATE NEW TASK FOR ' + o.id); if (o.Deal_Queue_Status__c == 'DNR') { System.debug('triggerfire'); tsk = new Task( whatid=o.Id, OwnerId=o.ownerid, Subject='ACTION REQUIRED - Your Signed Merchant has been UNSCHEDULED', RecordTypeId = RT.Id ); insert tsk; } } }
And the follow up trigger is
trigger UpdateUnschedOpp on Task (before update) { if(Trigger.new.size() == 1 ) { Task tk = Trigger.New[0]; String str = tk.whatid; if(str != null && str.substring(0,3)== '006') { Opportunity opp = [select Id, Merchant_willing_to_run__c, Requested_Run_Month__c from Opportunity where Id = :tk.WhatId ]; List<Task> tsks = [Select Merchant_willing_to_run__c, Requested_Run_Month__c From Task where whatid=:tk.whatid and what.type = 'Opportunity' and isClosed = false and Merchant_willing_to_run__c != null]; if (tsks.size()>0) { opp.Merchant_willing_to_run__c = tsks[0].Merchant_willing_to_run__c; opp.Requested_Run_Month__c = tsks[0].Requested_Run_Month__c; } update opp; } } }
Any help at all would be great, I am at an impasse and need to figure this out ASAP.
Thanks a lot all!
Alex Roth
First : take out the "update opp " in the 2nd trigger... it will work
second if doesnt
Go to the debug logs ,open the last log....search for the part of your error....see which trigger is obstructing it .....Next if its not that go to the workflows and check the workflows on the object.Deactivate the particular workflow it should work 4 sure..........
did it work..????
Steve,
If I take the update opp part out of the second trigger then it does not update the opportunity as I need the trigger to do.
If I take out update from the initial trigger, then the initial trigger will never fire, as records are never inserted with a field value as such.
I am looking into any workflows that may be firing, but it doesn;t seem as though this is the issue.
Thanks for the effort though -
Alex
ya it will update...........dont worry........take out the update opp in 2nd trigger because you are writing on a before update trigger it will
check other triggers on this object where you r updating the saame obj twice
Steve,
Here is the debug log, it fires several triggers, but the issue seems to stem from these two that I have afforementioned -
check this trigger once "