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
alok29novalok29nov 

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){           

      if(opp.ownerid==usr.id){

                             opp.Previous_Owner__c=usr.Name;              

                             opp2.add(opp);                            

                     }}  }   }              

                 update opp2;              

  }

 

 

 

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

 

Thanks

~Alok

VishwanathVishwanath

Hi please try this it might be help u

 

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

alok29novalok29nov

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.

 

~Alok

VishwanathVishwanath

Hi,

 

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;

for(opportunity o:Trigger.new)

{

opid=o.id;

}

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

alok29novalok29nov

I understand but i have to update those records which are in trigger.new 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 trigger.new 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.

 

~Alok

steve456steve456

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

alok29novalok29nov

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

steve456steve456

Try this

 

 

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

alok29novalok29nov

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.

 

Thanks,

Alok

steve456steve456

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

alok29novalok29nov

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.

 

Thanks,

Alok

steve456steve456

chnge it to

 

 newOpp.PreviousOwner__c =  oldOp​p.OwnerId;

Ramesh SomalagariRamesh Somalagari
Hi All I have same problem.Can you please click this link  http://salesforce.stackexchange.com/questions/43871/system-dmlexception-delete-failed-self-reference-from-trigger See the log I a have got same issue.