1

In my Code the scenario is there is a Custom objects in which two custom formula field which is dependent on other three custom fields and i have to update two other field with that formula field value. so for that i write a trigger class like this way:-

    public class RtrFieldsOnLegalRecord {

    public static void outstandingFieldUpdate(List<Legal__c> newLegals){

        List<String> legalAccnt=new List<String>();
        List<Legal__c> lgl = new List<Legal__c>();



        List<Legal__c> data=[Select id,Outstanding_Final_Judgment_Amount__c,Outstanding_Final_Settlement_Amount__c From Legal__c where Customer__c =: legalAccnt];
        System.debug('legal data ++=='+data);
        System.debug('trigger legal++=='+newLegals);
        for(Legal__c legal : newLegals){
            for(Legal__c leg : data){
                if(legal.Final_Judgment_Amount__c !=Null && legal.Final_Settlement_Amount__c !=Null){
                    leg.Outstanding_Final_Judgment_Amount__c = legal.Final_Judgment_Amount__c;
                    leg.Outstanding_Final_Settlement_Amount__c = legal.Final_Settlement_Amount__c;
                    System.debug('list legal++=='+lgl.size());
                    if(lgl.size()==0)
                         lgl.add(leg);
                }
            }
           update lgl;            
        }
}

and i called that class in before update, but by doing i am not able to update fields.

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

1 Answer 1

1

You cannot update the same record in the Before Update trigger with DML. Simply remove that update lgl; from your code. In the Before context records get updated without need of DML.

This is nicely explained for insert triggers in this post which we can apply to update triggers as well.

First take a look at Triggers and Order of Execution, and second review this statement:

Means when you use before insert update operation records are not committed to database so If we change the trigger context record's value then we don't need perform DML.

2
  • 2
    Try to quote the important part, in case that post is deleted at some point :) Aug 2, 2018 at 9:17
  • @JavierGarcíaManzano Thanks for advice, updated my answer
    – Eduard
    Aug 2, 2018 at 10:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .