I have written the trigger on service object.but i got error,i know updating the record on same object.how to overcome this type of error ?

my error is Like::

Error:Apex trigger updateaccounting caused an unexpected exception, contact your administrator: updateaccounting: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0G4E000000BTRKUA4; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a0G4E000000BTRK) is currently in trigger updateaccounting, therefore it cannot recursively update itself: []: Trigger.updateaccounting: line 48, column 1

    trigger updateaccounting on Service__c (before insert,before update) {

    string custclass;
    string prodclass;
    string acccustclass;
    string accprodclass;
    string strServiceId;

    List<Service__c>lstservices = new List<Service__c>();
    List<Service__c> lstservicesUpdate = new List<Service__c>();
    List<Accounting__c> lstaccounting = new List<Accounting__c>();
    List<Service__c>lstservices1 = new List<Service__c>();
    lstservices1 = [select Car_set__r.Order__r.Account.Customer_VAT_type__c,CB_Services_Repository__r.Rfleet_Product_classification__c from Service__c where id IN :Trigger.new];

    system.debug('lstservices1 >>>>>>>>>'+lstservices1);
    for(Service__c service : lstservices1 ){
        custclass = service.Car_set__r.Order__r.Account.Customer_VAT_type__c;
        prodclass =service.CB_Services_Repository__r.Rfleet_Product_classification__c;
        system.debug('product classification values>>>>>'+prodclass+custclass );

        lstaccounting = [select Rfleet_Customer_Tax__c,Rfleet_Product_Tax__c from Accounting__c];

        for(Accounting__c accounting : lstaccounting ){

            system.debug('Accounting values>>>'+accounting);
            acccustclass = accounting.Rfleet_Customer_Tax__c;
            system.debug('accountcustomerclassfication values>>>>'+acccustclass);
            accprodclass = accounting.Rfleet_Product_Tax__c ;
            system.debug('accountproductclassfication values>>>'+accprodclass);

            if(acccustclass == custclass && accprodclass == prodclass){

                system.debug('>>>>>>>>>inside if>>');
                service.Rfleet_Accounting__c =accounting.id;

                system.debug('>>>>>>>>>'+accounting .id);


    update lstservicesUpdate;
  • What are you trying to achieve? >>>>SOQL query inside for loop<<<< Also as @mr. frodo said , no need for DML in before triggers? – C0DEPirate May 5 '16 at 11:59

You are trying to update the Service__c list in the before update itself.That is not required.

You cannot recursively update or delete the same object from an Apex trigger. This error often occurs when: A trigger performs a DML operation that ends up updating a record that is already being updated by another trigger. It must not cause a DML event to occur on a record already involved in the same DML event. For example, it is okay to update the record during an insert, and it is okay to delete during an update, but you cannot update inside an update (this includes the upsert or convert command).

This error occurs with both direct and indirect operations. The following is an example of an indirect operation:

  1. A request is submitted to update Object A.
  2. A before update trigger on object A creates an object B.
  3. Object A is updated.
  4. An after insert trigger on object B queries object A and updates it. This is an indirect update of object A because of the before trigger of object A, so an error is generated.
  • Hi,Mr.Frodo.thanks for your reply.how to rectify that error .please suggest me.where i have to change my code. – kiran May 6 '16 at 6:38
  • You just have to use the Trigger.newlist of Service__c.Make whatever changes you want to do in that and remove the DML(Update) statement.It will automatically update or insert it with the changes you want. – Mr.Frodo May 6 '16 at 6:45

Your Answer

