+ Start a Discussion
Ramadhar MishraRamadhar Mishra 

SELF_REFERENCE_FROM_TRIGGER

I am getting the following error message : Please some one help me to getrid off this problem .

 

 

Error:

Update failed. First exception on row 0 with id 500Q0000002FHIoIAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RecordType_Change: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 500Q0000002FHIoIAO; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 500Q0000002FHIo) is currently in trigger RecordType_Change, therefore it cannot recursively update itself: [] Trigger.RecordType_Change: line 12, column 16: []

 

 

trigger RecordType_Change on Case (before Update) {
IF(Trigger.isUpdate){
IF(!CaseRecordTypeUpdate.flag){
List<Case> listCaseold = [Select Id,Accountability__c,Accountable_Agent_VF__c,RecordType.Name from Case where ID =:Trigger.Old[0].ID];
List<Case> listCasenew = [Select Id,Accountability__c,Cause_Level_1__c,Accountable_Agent_VF__c,RecordType.Name from Case where ID =:Trigger.new[0].ID];       
IF(listCaseold[0].RecordType.Name.equalsIgnoreCase('csi')){
          IF(listCasenew[0].Accountability__c.equalsIgnoreCase('AXP') && listCasenew[0].Cause_Level_1__c.equalsIgnoreCase('Service Quality')){    
               RecordType objRec=[Select Id from RecordType where Name =: 'quick feedback'];
               listCasenew[0].RecordType.Name=objRec.ID;
               System.debug('Record Type  :'+objRec.ID);
               listCasenew[0].Accountable_Agent2__c=listCaseold[0].Accountable_Agent_VF__c;                 
               update listCasenew[0];  
     }
   }
}
CaseRecordTypeUpdate.flag=false;
}
}

 

Thanks,

Ram

bob_buzzardbob_buzzard

You aren't allowed to execute update on listCaseNew[0] as that is already part of the trigger.  In fact there is no need to do that, as you can simply make changes to it and these will be saved to the database when the transaction commits.

 

A before update trigger allows you to make further changes before the record is committed to the database.

Ramadhar MishraRamadhar Mishra

I made the chages. But still it is giving me the same issue.

 

Error:

Update failed. First exception on row 0 with id 500Q0000002FHIoIAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RecordType_Change: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 500Q0000002FHIoIAO; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 500Q0000002FHIo) is currently in trigger RecordType_Change, therefore it cannot recursively update itself: [] Trigger.RecordType_Change: line 13, column 5: []

 

 

 

My Trigger Code :

 

trigger RecordType_Change on Case (before Update) {
IF(Trigger.isUpdate){
IF(CaseRecordTypeUpdate.flag){
CaseRecordTypeUpdate.flag=false;
Case objCaseold = [Select Id,Accountability__c,Cause_Level_1__c,Accountable_Agent_VF__c,RecordType.Name from Case where ID =:Trigger.Old[0].ID];       
IF(objCaseold.RecordType.Name.equalsIgnoreCase('csi')){
Case objCasenew = [Select Id,Accountability__c,Cause_Level_1__c,Accountable_Agent_VF__c,RecordType.Name from Case where ID =:Trigger.new[0].ID];       
 IF(objCasenew.Accountability__c.equalsIgnoreCase('AXP') && objCasenew.Cause_Level_1__c.equalsIgnoreCase('Service Quality')){    
    RecordType objRec=[Select Id from RecordType where Name =: 'quick feedback'];
    objCasenew.RecordType.Name=objRec.ID;
    System.debug('Record Type  :'+objRec.ID);
    objCasenew.Accountable_Agent2__c=objCaseold.Accountable_Agent_VF__c;                 
    update objCasenew ;  
   }
 }
}

}
}

 

Regards,

Ram

Ramadhar MishraRamadhar Mishra

Please help me to get resolve the issue.

 

Thanks in Advance !!

_Prasu__Prasu_

Quick fix can be converting the trigger to after event.

 

Exception is due to updating the same object object on which trigger is fire through DML update statement.

update objCasenew ;  

i guess above statement is causing the problem.

BritishBoyinDCBritishBoyinDC

Bob's point is key = the record you are trying to update is already in Context, so you can't update it in the trigger with an explicit update call - a before update trigger is in the process of updating a record, so you are in effect just making some additional changes before it is committed to the database

 

Which raises a bigger question...why are you executing a Select for the Case based on Trigger.New/Trigger.Old? Trigger.New and Trigger.Old contains the whole Case record, so you can do your comparison based on the records passed into the trigger. Just loop through the records in Trigger.new, and that way, you can avoid hard coding the [0] reference, as that means this will never work in bulk...you might only be updating 1 most of the time, but you should still be coding for bulk. 

b-Forceb-Force

Ram,

I have update the trigger code as follow

As we can get

no need to any update statement o0ver there,

Also you will get copy of old and New Lead Records, so no need to Query

check out following code ..

 

trigger RecordType_Change on Case (before Update) {
IF(Trigger.isUpdate){
IF(CaseRecordTypeUpdate.flag){
CaseRecordTypeUpdate.flag=false;
Case objCaseold = [Select Id,Accountability__c,Cause_Level_1__c,Accountable_Agent_VF__c,RecordType.Name from Case where ID =:Trigger.Old[0].ID];       
IF(objCaseold.RecordType.Name.equalsIgnoreCase('csi')){
//Case objCasenew = [Select Id,Accountability__c,Cause_Level_1__c,Accountable_Agent_VF__c,RecordType.Name from Case where ID =:Trigger.new[0].ID];       
 IF(Trigger.new[0].Accountability__c.equalsIgnoreCase('AXP') && Trigger.new[0].Cause_Level_1__c.equalsIgnoreCase('Service Quality')){    
    RecordType objRec=[Select Id from RecordType where Name =: 'quick feedback'];
    Trigger.new[0].RecordType.Name=objRec.ID;
    System.debug('Record Type  :'+objRec.ID);
    Trigger.new[0].Accountable_Agent2__c=objCaseold.Accountable_Agent_VF__c;                 
//    update objCasenew ;  
   }
 }
}

}
}

 

 

this will work , and will fullfill your bussiness requirement

 

Thanks,

Bala

 

BritishBoyinDCBritishBoyinDC

You're still hard coding in the [0] reference, which is frowned upon by Salesforce...

 

You should use a construct like this so you can reference both old and new records in the trigger in bulk

 

 

trigger newold_example on Case (before Update) {


for (Integer i = 0; i < Trigger.New.Size(); i++) {

 if(Trigger.new[0].Subject.equalsIgnoreCase('Test')){    
    Trigger.new[0].Subject = Trigger.Old[i].EngineeringReqNumber__c;                 
 }
}

}

 

 

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-first-exception-on-row-0-with-id See the log I a have got same issue.