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