function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

SELF_REFERENCE_FROM_TRIGGER error in a trigger..Urgent help needed

Hi All,


I have written a query where I want to update a field on opportunity if Opportunity Owner of any opportunity is changed. I have a field Previous_Owner__c on opportunity which stores the name of previous owner if owner is changed.

I am getting the below error:

"Apex trigger OpptyPreOwnAssn caused an unexpected exception, contact your administrator: OpptyPreOwnAssn: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006P0000003XNGsIAO; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 006P0000003XNGs) is currently in trigger OpptyPreOwnAssn, therefore it cannot recursively update itself".



trigger OpptyPreOwnAssn on Opportunity ( before update) {

     List <Opportunity> listOppty= [Select Id,OwnerID, Previous_Owner__c from Opportunity where Id In:Trigger.New];

    Set<ID> Owner1id = new Set<ID>();


    List<Opportunity> opp2= new List<opportunity>();      


     for(Opportunity opp1 : listOppty){                 

       Owner1id.add(opp1.OwnerId);   }  


     List< User > listusr = [Select Id,Name from User where ID in : Owner1id];         



for (ID id1:Owner1ID){     

 for (Opportunity opp:listOppty){                

   for (User usr:listusr){           




                     }}  }   }              

                 update opp2;              





I am not able to figure out why I am getting the  error. Can anybody please suggest the remedy for the above.





Hi please try this it might be help u


   List <Opportunity> listOppty= [Select Id,OwnerID, Previous_Owner__c from Opportunity where Id !=:Trigger.New];


That did not help. And  even it is not correct way to write a query where Id !=:Trigger.New. It will give an error. I used trigger.old instead of that but getting the same error.






This Problem is occures when u updating list, and the list contains the record wich ur going to update,

remove that record from the list and try


id opid;




 List <Opportunity> listOppty= [Select Id,OwnerID, Previous_Owner__c from Opportunity where Id not  In:opid];


I understand but i have to update those records which are in instance. Let me explain. If I change the opportunity owner of Opportunity A, then opportunity A's Previous_Owner__c field will have the name of previous owner.

So, in this case Opportnity A will be in instance. If i remove that record from list, how wil I update that oportunity.


Can u give me working code only Previous_Owner__c is a custom filed and all others are standard filed either of Opportunity or User.




Take out the "update opp2""...because u r writinga trigger on before update so it shuld work


I have tried that earlier  but  i am getting  owner id not owner name in Previous_Owner__c field.


Try this



trigger setPreviousOwner on Opportunity (before insert,before update) {
    for(Integer index=0; index <; index++) {
        Opportunity  newOpp =[index];
            newOpp.PreviousOwner__c =  newOpp.OwnerId;
            Opportunity oldOpp = Trigger.old[index];
            if(newOpp.OwnerId != oldOpp.OwnerId)
                newAssoc.PreviousOwner__c =  oldOpp.OwnerId;


Hi Steve,


Code is working fine but my requirement is to populate Previous Owner name not owner id. Your code is populating owner id not owner name.





as far as i know i have the same req it populates the name for me and not the id.....


In my org, it is populating owned id only. and that should be the case as you are assigning OwnerID only.


 newAssoc.PreviousOwner__c =  oldOp​p.OwnerId;


I am a bit puzzled how your field is getting pouplated with name.





chnge it to


 newOpp.PreviousOwner__c =  oldOp​p.OwnerId;

Ramesh SomalagariRamesh Somalagari
Hi All I have same problem.Can you please click this link See the log I a have got same issue.