I am not able to perform insertion,updation,deletion on account
Apex class:
public static void parentUpdate(list<Contact> records){
set<ID> ids = new set<ID>();
for(Contact c: records){
if(c.Amount__c != Null){
ids.add(c.AccountId);
}
}
list<Account> apayList=[select id,Total_Amount__c,(select Amount__c from Contacts) from Account where id IN:ids];
for(Account ac:apayList){
double amount=0;
for(Contact co:ac.Contacts){
if(ac.Contacts.size()>0 && co.AccountId!=null){
amount+=co.Amount__c;
}
}
ac.Total_Amount__c =amount;
}
update apayList;
system.debug('#####'+apayList);
}
@future
public static void countparentupdate(Id recordId){
list<Contact> records= [select id,AccountId,Amount__c from Contact WHERE
AccountId = :recordId];
parentUpdate(records);
}
Apex Trigger:
trigger updateAccount on Contact (after insert,after update,after delete) {
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
for(Contact c :Trigger.new){
AccountTotalAmount.countparentupdate(c.AccountId);
}
}
if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isDelete)){
for(Contact c :Trigger.old){
AccountTotalAmount.countparentupdate(c.AccountId);
}
}
}
Stack Trace Error:
updateAccount: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0017F00001odBxkQAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Contactupdate: maximum trigger depth exceeded Contact trigger event AfterUpdate Account trigger event AfterUpdate Contact trigger event AfterUpdate Account trigger event AfterUpdate Contact trigger event AfterUpdate Account trigger event AfterUpdate Contact trigger event AfterUpdate Account trigger event AfterUpdate Contact trigger event AfterUpdate Account trigger event AfterUpdate Contact trigger event AfterUpdate Account trigger event AfterUpdate Contact trigger event AfterUpdate Account trigger event AfterUpdate Contact trigger event AfterUpdate Account trigger event AfterUpdate: [] Class.AccountTotalAmount.parentUpdate: line 51, column 1 Class.AccountTotalAmount.countparentupdate: line 59, column 1 Trigger.updateAccount: line 5, column 1